This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy path3-bush-berries.js
More file actions
51 lines (38 loc) · 1.59 KB
/
3-bush-berries.js
File metadata and controls
51 lines (38 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
The space travellers have safely landed and are foraging for food in the natural wildlife.
There are bushes with many different colour berries.
The pink berries are the ONLY safe ones to eat.
If any other berries are present, it's best not to eat from the bush at all!
Create a function which checks if the bush has ALL PINK berries and is safe for the astronauts to eat from the bush.
Use the tests to confirm which message to return
This exercise can be solved in a few different ways. One way might include the array methods
.some() and .every().
The .some() method tests to see if some of the values (at least 1) in an array
match what you're looking for and returns true or false.
The .every() method will only return true if all values match watch you're looking for.
Let's first look at an example that will teach you how to use these methods.
*/
function isBushSafe(berryArray) {
if (berryArray.every() == "pink") {
return "Bush is safe to eat";
} else {
return "Toxic! Leave bush alone!";
}
//Write your code here
}
const bushIsSafe = berryArray.every((color) => color == "pink");
const output = bushIsSafe
? "Bush is safe to eat from"
: "Toxic! Leave bush alone!";
return output;
/* ======= TESTS - DO NOT MODIFY ===== */
test("isBushSafe finds toxic busy", () => {
expect(
isBushSafe(["pink", "pink", "pink", "neon", "pink", "transparent"])
).toEqual("Toxic! Leave bush alone!");
});
test("isBushSafe function finds safe bush", () => {
expect(isBushSafe(["pink", "pink", "pink", "pink"])).toEqual(
"Bush is safe to eat from"
);
});