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

Commit aebc617

Browse files
authored
Improve JS1 week2 exercise 2 (#15)
* Show expected/actual - this both helps the students debug, and better matches what they should see professionally from tests. * Make question about removing element less misleading (its old wording suggests modifying the array, before telling you not to). * Add decimal edge case.
1 parent 6e8f73b commit aebc617

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

week-2/2-mandatory/2-function-creation.js

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ Tip: use logical operators
1818
function validate(num) {}
1919

2020
/*
21-
Write a function that removes an element from an array
22-
The function must:
23-
- NOT change the original array
24-
- return a new array with the item removed
25-
- remove the item at the specified index
21+
Write a function that returns a copy of the given array arr, but with the element at the given index, index removed.
22+
The function must NOT change the original array, arr.
2623
*/
2724

2825
function remove(arr, index) {
@@ -43,6 +40,27 @@ function formatPercentage(arr) {
4340

4441
/* ======= TESTS - DO NOT MODIFY ===== */
4542

43+
const util = require('util');
44+
45+
function test(test_name, actual, expected) {
46+
let status;
47+
48+
let isEqual;
49+
if (Array.isArray(expected)) {
50+
isEqual = arraysEqual(actual, expected);
51+
} else {
52+
isEqual = actual === expected;
53+
}
54+
55+
if (isEqual) {
56+
status = "PASSED";
57+
} else {
58+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
59+
}
60+
61+
console.log(`${test_name}: ${status}`);
62+
}
63+
4664
function arraysEqual(a, b) {
4765
if (a === b) return true;
4866
if (a == null || b == null) return false;
@@ -55,62 +73,49 @@ function arraysEqual(a, b) {
5573
return true;
5674
}
5775

58-
function test(test_name, expr) {
59-
let status;
60-
if (expr) {
61-
status = "PASSED";
62-
} else {
63-
status = "FAILED";
64-
}
65-
66-
console.log(`${test_name}: ${status}`);
67-
}
68-
6976
test(
7077
"tidyUpString function works - case 1",
71-
arraysEqual(tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [
78+
tidyUpString(["/Daniel ", "irina ", " Gordon", "ashleigh "]), [
7279
"daniel",
7380
"irina",
7481
"gordon",
7582
"ashleigh"
76-
])
83+
]
7784
);
7885
test(
7986
"tidyUpString function works - case 2",
80-
arraysEqual(
81-
tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]),
82-
["sanyia", "michael", "anthony", "tim"]
83-
)
87+
tidyUpString([" /Sanyia ", " Michael ", "AnTHonY ", " Tim "]),
88+
["sanyia", "michael", "anthony", "tim"]
8489
);
8590

86-
test("validate function works - case 1", validate(10) === true);
87-
test("validate function works - case 2", validate(18) === true);
88-
test("validate function works - case 3", validate(17) === false);
89-
test("validate function works - case 4", validate("Ten") === false);
90-
test("validate function works - case 5", validate(108) === false);
91+
test("validate function works - case 1", validate(10), true);
92+
test("validate function works - case 2", validate(18), true);
93+
test("validate function works - case 3", validate(17), false);
94+
test("validate function works - case 4", validate("Ten"), false);
95+
test("validate function works - case 5", validate(108), false);
9196

9297
test(
9398
"remove function works - case 1",
94-
arraysEqual(remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29])
99+
remove([10, 293, 292, 176, 29], 3), [10, 293, 292, 29]
95100
);
96101
test(
97-
"remove function works - case 1",
98-
arraysEqual(remove(["a", "b", "c", "d", "e", "f", "g"], 6), [
102+
"remove function works - case 2",
103+
remove(["a", "b", "c", "d", "e", "f", "g"], 6), [
99104
"a",
100105
"b",
101106
"c",
102107
"d",
103108
"e",
104109
"f"
105-
])
110+
]
106111
);
107112

108113
test(
109114
"formatPercentage function works - case 1",
110-
arraysEqual(formatPercentage([23, 18, 187.2, 0.372]), [
115+
formatPercentage([23, 18.103, 187.2, 0.372]), [
111116
"23%",
112-
"18%",
117+
"18.1%",
113118
"100%",
114119
"0.37%"
115-
])
116-
);
120+
]
121+
);

0 commit comments

Comments
 (0)