Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.

Commit 6e8f73b

Browse files
authored
Improve JS1 week2 exercise 1 (#14)
* All functions take arguments - artificial examples which don't involve taking input make it less clear to students why functions are useful. * Show expected/actual - this both helps the students debug, and better matches what they should see professionally from tests. * Test that first5 used slice not splice.
1 parent 1ffdd46 commit 6e8f73b

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

week-2/2-mandatory/1-fix-functions.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,23 @@ function mood() {
1111
}
1212
}
1313

14-
function greaterThan10() {
15-
let num = 10;
14+
function greaterThan10(num) {
1615
let isBigEnough;
1716

1817
if (isBigEnough) {
19-
return "num is greater than or equal to 10";
18+
return "num is greater than 10";
2019
} else {
2120
return "num is not big enough";
2221
}
2322
}
2423

25-
function sortArray() {
26-
let letters = ["a", "n", "c", "e", "z", "f"];
27-
let sortedLetters;
24+
function sortArray(letters) {
25+
let sortedLetters = letters;
2826

2927
return sortedLetters;
3028
}
3129

32-
function first5() {
33-
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
30+
function first5(numbers) {
3431
let sliced;
3532

3633
return sliced;
@@ -45,12 +42,22 @@ function get3rdIndex(arr) {
4542

4643
/* ======= TESTS - DO NOT MODIFY ===== */
4744

48-
function test(test_name, expr) {
45+
const util = require('util');
46+
47+
function test(test_name, actual, expected) {
4948
let status;
50-
if (expr) {
49+
50+
let isEqual;
51+
if (Array.isArray(expected)) {
52+
isEqual = arraysEqual(actual, expected);
53+
} else {
54+
isEqual = actual === expected;
55+
}
56+
57+
if (isEqual) {
5158
status = "PASSED";
5259
} else {
53-
status = "FAILED";
60+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
5461
}
5562

5663
console.log(`${test_name}: ${status}`);
@@ -68,23 +75,38 @@ function arraysEqual(a, b) {
6875
return true;
6976
}
7077

71-
test("mood function works", mood() === "I am not happy");
78+
test("mood function works for true", mood(true), "I am happy");
79+
test("mood function works for false", mood(false), "I am not happy");
7280
test(
73-
"greaterThanTen function works",
74-
greaterThan10() === "num is greater than or equal to 10"
81+
"greaterThanTen function works for 11",
82+
greaterThan10(11), "num is greater than 10"
83+
);
84+
test(
85+
"greaterThanTen function works for 10",
86+
greaterThan10(10), "num is not big enough"
87+
);
88+
test(
89+
"greaterThanTen function works for 9",
90+
greaterThan10(9), "num is not big enough"
7591
);
7692
test(
7793
"sortArray function works",
78-
arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"])
94+
sortArray(["a", "n", "c", "e", "z", "f"]), ["a", "c", "e", "f", "n", "z"]
7995
);
80-
test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5]));
96+
97+
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
98+
test("first5 function works", first5(numbers), [1, 2, 3, 4, 5]);
99+
if (!arraysEqual(numbers, [1, 2, 3, 4, 5, 6, 7, 8])) {
100+
console.log("PROBLEM: first5 changed its input array - it shouldn't!")
101+
}
81102

82103
test(
83104
"get3rdIndex function works - case 1",
84-
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) ===
85-
"strawberry"
105+
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]),
106+
"strawberry"
86107
);
87108
test(
88109
"get3rdIndex function works - case 2",
89-
get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18
110+
get3rdIndex([11, 37, 62, 18, 19, 3, 30]),
111+
18
90112
);

0 commit comments

Comments
 (0)