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 path2-oxygen-levels.js
More file actions
47 lines (38 loc) · 1.59 KB
/
2-oxygen-levels.js
File metadata and controls
47 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
/*
Many years into the future, a team of Space Voyagers find their ship is low on Oxygen and need to dock
somewhere safe while they call home for help.
Their computer detects a list of nearby planets that have Oxygen in their atmosphere.
To be safe, they need to land on the first unnamed planet that has Oxygen levels between 19.5% and 23.5%.
Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%
Some string methods that might help you here are .replace() and .substring().
*/
function findSafeOxygenLevel(oxygen) {
let firstSafePlanet = oxygen
.filter((planet) => planet.includes("%"))
.map((planet) => Number(planet.replace("%", "")))
.find((planet) => planet > 19.5 && planet < 23.5);
if (firstSafePlanet === undefined) {
return firstSafePlanet;
} else {
return firstSafePlanet + "%";
}
}
/* ======= TESTS - DO NOT MODIFY ===== */
test("findSafeOxygenLevel function works - case 1", () => {
expect(
findSafeOxygenLevel(["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"])
).toEqual("19.9%");
});
test("findSafeOxygenLevel function works - case 2", () => {
expect(
findSafeOxygenLevel(["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"])
).toEqual("20.2%");
});
test("findSafeOxygenLevel function filters out invalid percentages", () => {
expect(
findSafeOxygenLevel(["200%", "-21.5%", "20", "apes", "21.1%"])
).toEqual("21.1%");
});
test("findSafeOxygenLevel function returns undefined if no valid planets found", () => {
expect(findSafeOxygenLevel(["50"])).toBeUndefined();
});