This repository was archived by the owner on Oct 26, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path1-fix-functions.js
More file actions
87 lines (74 loc) · 1.84 KB
/
1-fix-functions.js
File metadata and controls
87 lines (74 loc) · 1.84 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// The below functions are syntactically correct but not outputting the right results.
// Look at the tests and see how you can fix them.
function mood(isHappy) {
// let isHappy = true;
if (isHappy) {
return "I am happy";
}
return "I am not happy";
}
function greaterThan10(isBigEnough) {
let num = 10;
// let isBigEnough;
if (num >= 10) {
return "num is greater than or equal to 10";
} else {
return "num is not big enough";
}
}
function sortArray() {
const letters = ["a", "n", "c", "e", "z", "f"];
let sortedLetters;
sortedLetters = letters.sort();
return sortedLetters;
}
function first5() {
const numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let sliced;
sliced = numbers.slice(0, 5);
return sliced;
}
function get3rdIndex(arr) {
const index = 3;
let element;
element = arr[index];
return element;
}
/* ======= TESTS - DO NOT MODIFY ===== */
function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
test("mood function works", mood() === "I am not happy");
test(
"greaterThanTen function works",
greaterThan10() === "num is greater than or equal to 10"
);
test(
"sortArray function works",
arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"])
);
test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5]));
test(
"get3rdIndex function works - case 1",
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) ===
"strawberry"
);
test(
"get3rdIndex function works - case 2",
get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18
);