|
2 | 2 | // Look at the tests and see how you can fix them.
|
3 | 3 |
|
4 | 4 | function mood() {
|
5 |
| - let isHappy = true; |
| 5 | + let isHappy = false; |
6 | 6 |
|
7 |
| - if (isHappy) { |
8 |
| - return "I am happy"; |
9 |
| - } else { |
10 |
| - return "I am not happy"; |
11 |
| - } |
| 7 | + if (isHappy) { |
| 8 | + return "I am happy"; |
| 9 | + } else { |
| 10 | + return "I am not happy"; |
| 11 | + } |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | function greaterThan10() {
|
15 |
| - let num = 10; |
16 |
| - let isBigEnough; |
17 |
| - |
18 |
| - if (isBigEnough) { |
19 |
| - return "num is greater than or equal to 10"; |
20 |
| - } else { |
21 |
| - return "num is not big enough"; |
22 |
| - } |
| 15 | + let num = 10; |
| 16 | + let isBigEnough = true; |
| 17 | + |
| 18 | + if (isBigEnough) { |
| 19 | + return "num is greater than or equal to 10"; |
| 20 | + } else { |
| 21 | + return "num is not big enough"; |
| 22 | + } |
23 | 23 | }
|
24 | 24 |
|
25 |
| -function sortArray() { |
26 |
| - let letters = ["a", "n", "c", "e", "z", "f"]; |
27 |
| - let sortedLetters; |
| 25 | +function sortArray(a, b) { |
| 26 | + let letters = ["a", "n", "c", "e", "z", "f"]; |
28 | 27 |
|
29 |
| - return sortedLetters; |
| 28 | + let sortedLetters = letters.sort(a, b); |
| 29 | + |
| 30 | + return sortedLetters; |
30 | 31 | }
|
31 | 32 |
|
32 | 33 | function first5() {
|
33 |
| - let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; |
34 |
| - let sliced; |
| 34 | + let numbers = [1, 2, 3, 4, 5, 6, 7, 8]; |
| 35 | + let sliced = numbers.slice(0, 5); |
35 | 36 |
|
36 |
| - return sliced; |
| 37 | + return sliced; |
37 | 38 | }
|
38 | 39 |
|
39 | 40 | function get3rdIndex(arr) {
|
40 |
| - let index = 3; |
41 |
| - let element; |
| 41 | + let index = 3; |
| 42 | + let element = arr[index]; |
42 | 43 |
|
43 |
| - return element; |
| 44 | + return element; |
44 | 45 | }
|
45 | 46 |
|
46 | 47 | /* ======= TESTS - DO NOT MODIFY ===== */
|
47 | 48 |
|
48 | 49 | function test(test_name, expr) {
|
49 |
| - let status; |
50 |
| - if (expr) { |
51 |
| - status = "PASSED"; |
52 |
| - } else { |
53 |
| - status = "FAILED"; |
54 |
| - } |
55 |
| - |
56 |
| - console.log(`${test_name}: ${status}`); |
| 50 | + let status; |
| 51 | + if (expr) { |
| 52 | + status = "PASSED"; |
| 53 | + } else { |
| 54 | + status = "FAILED"; |
| 55 | + } |
| 56 | + |
| 57 | + console.log(`${test_name}: ${status}`); |
57 | 58 | }
|
58 | 59 |
|
59 | 60 | function arraysEqual(a, b) {
|
60 |
| - if (a === b) return true; |
61 |
| - if (a == null || b == null) return false; |
62 |
| - if (a.length != b.length) return false; |
| 61 | + if (a === b) return true; |
| 62 | + if (a == null || b == null) return false; |
| 63 | + if (a.length != b.length) return false; |
63 | 64 |
|
64 |
| - for (let i = 0; i < a.length; ++i) { |
65 |
| - if (a[i] !== b[i]) return false; |
66 |
| - } |
| 65 | + for (let i = 0; i < a.length; ++i) { |
| 66 | + if (a[i] !== b[i]) return false; |
| 67 | + } |
67 | 68 |
|
68 |
| - return true; |
| 69 | + return true; |
69 | 70 | }
|
70 | 71 |
|
71 | 72 | test("mood function works", mood() === "I am not happy");
|
72 | 73 | test(
|
73 |
| - "greaterThanTen function works", |
74 |
| - greaterThan10() === "num is greater than or equal to 10" |
| 74 | + "greaterThanTen function works", |
| 75 | + greaterThan10() === "num is greater than or equal to 10" |
75 | 76 | );
|
76 | 77 | test(
|
77 |
| - "sortArray function works", |
78 |
| - arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"]) |
| 78 | + "sortArray function works", |
| 79 | + arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"]) |
79 | 80 | );
|
80 | 81 | test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5]));
|
81 | 82 |
|
82 | 83 | test(
|
83 |
| - "get3rdIndex function works - case 1", |
84 |
| - get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) === |
85 |
| - "strawberry" |
| 84 | + "get3rdIndex function works - case 1", |
| 85 | + get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) === |
| 86 | + "strawberry" |
86 | 87 | );
|
87 | 88 | test(
|
88 |
| - "get3rdIndex function works - case 2", |
89 |
| - get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18 |
| 89 | + "get3rdIndex function works - case 2", |
| 90 | + get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18 |
90 | 91 | );
|
0 commit comments