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

Commit 1ffdd46

Browse files
authored
Improvements to JS1 week1 homework (#13)
* Tests show what was expected and what was actually returned. This both helps the students to understand what was wrong, and is more like what they'll encounter in real life from test suites. * Fix up partial rename from getRemainder to getTotal. * Fix up real-world logic of currency conversion (a % fee should _cost_ you more, not _give_ you more!) * Magic 8 ball doesn't use a global variable, and explains slightly more what checkAnswer should actually do. * Magic 8 ball tests to make sure more than one value is actually returned. Note that this introduces use of `require` to import `util` - this is a built-in module in Node, so doesn't add any extra requirements to how the tests are run, but does introduce an element of syntax we haven't taught the students. This is only in the test machinery itself, and is fairly cosmetic, so hopefully won't confuse folks.
1 parent c8e0ebb commit 1ffdd46

File tree

7 files changed

+119
-104
lines changed

7 files changed

+119
-104
lines changed
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
// There are syntax errors in this code - can you fix it to pass the tests?
42

53
function addNumbers(a b c) {
@@ -9,28 +7,32 @@ function addNumbers(a b c) {
97
function introduceMe(name, age)
108
return "Hello, my name is " + name "and I am " age + "years old";
119

12-
function getAddition(a, b) {
13-
total = a ++ b
10+
function getTotal(a, b) {
11+
total = a ++ b;
1412

1513
// Use string interpolation here
1614
return "The total is %{total}"
1715
}
1816

19-
/* ======= TESTS - DO NOT MODIFY ===== */
20-
//
21-
// To run these tests type `node 1-syntax-errors.js` into your terminal
17+
/* ======= TESTS - DO NOT MODIFY =====
18+
There are some Tests in this file that will help you work out if your code is working.
19+
20+
To run these tests type `node 1-syntax-errors.js` into your terminal
21+
*/
22+
23+
const util = require('util');
2224

23-
function test(test_name, expr) {
25+
function test(test_name, actual, expected) {
2426
let status;
25-
if (expr) {
26-
status = "PASSED"
27+
if (actual === expected) {
28+
status = "PASSED";
2729
} else {
28-
status = "FAILED"
30+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
2931
}
3032

31-
console.log(`${test_name}: ${status}`)
33+
console.log(`${test_name}: ${status}`);
3234
}
3335

34-
test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13)
35-
test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old")
36-
test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3")
36+
test("fixed addNumbers function - case 1", addNumbers(3, 4, 6), 13);
37+
test("fixed introduceMe function", introduceMe("Sonjide", 27), "Hello, my name is Sonjide and I am 27 years old");
38+
test("fixed getTotal function", getTotal(23, 5), "The total is 28");

week-1/2-mandatory/2-logic-error.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,25 @@ function multiply(a, b, c) {
1313
return;
1414
}
1515

16-
/* ======= TESTS - DO NOT MODIFY =====
16+
/* ======= TESTS - DO NOT MODIFY =====
1717
There are some Tests in this file that will help you work out if your code is working.
1818
1919
To run these tests type `node 2-logic-error` into your terminal
2020
*/
2121

22-
function test(test_name, expr) {
23-
let status;
24-
if (expr) {
25-
status = "PASSED"
26-
} else {
27-
status = "FAILED"
28-
}
22+
const util = require('util');
2923

30-
console.log(`${test_name}: ${status}`)
24+
function test(test_name, actual, expected) {
25+
let status;
26+
if (actual === expected) {
27+
status = "PASSED";
28+
} else {
29+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
30+
}
31+
32+
console.log(`${test_name}: ${status}`);
3133
}
3234

33-
test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture")
34-
test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25)
35-
test("fixed multiply function", multiply(2,3,6) === 36)
35+
test("fixed trimWord function", trimWord(" CodeYourFuture "), "CodeYourFuture");
36+
test("fixed wordLength function", getWordLength("A wild sentence appeared!"), 25);
37+
test("fixed multiply function", multiply(2, 3, 6), 36);

week-1/2-mandatory/3-function-output.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,40 @@ function s(w1, w2) {
99
}
1010

1111
function concatenate(firstWord, secondWord, thirdWord) {
12-
// Write the body of this function to concatenate three words together
13-
// Look at the test case below to understand what to expect in return
12+
// Write the body of this function to concatenate three words together.
13+
// Look at the test case below to understand what this function is expected to return.
1414
}
1515

16-
/* ======= TESTS - DO NOT MODIFY =====
16+
/* ======= TESTS - DO NOT MODIFY =====
1717
There are some Tests in this file that will help you work out if your code is working.
18-
1918
To run these tests type `node 3-function-output` into your terminal
2019
*/
2120

22-
function test(test_name, expr) {
23-
let status;
24-
if (expr) {
25-
status = "PASSED";
26-
} else {
27-
status = "FAILED";
28-
}
21+
const util = require('util');
22+
23+
function test(test_name, actual, expected) {
24+
let status;
25+
if (actual === expected) {
26+
status = "PASSED";
27+
} else {
28+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
29+
}
2930

30-
console.log(`${test_name}: ${status}`);
31+
console.log(`${test_name}: ${status}`);
3132
}
3233

3334
test(
3435
"concatenate function - case 1 works",
35-
concatenate("code", "your", "future") === "code your future"
36+
concatenate('code', 'your', 'future'),
37+
"code your future"
3638
);
3739
test(
3840
"concatenate function - case 2 works",
39-
concatenate("I", "like", "pizza") === "I like pizza"
41+
concatenate('I', 'like', 'pizza'),
42+
"I like pizza"
4043
);
4144
test(
4245
"concatenate function - case 3 works",
43-
concatenate("I", "am", 13) === "I am 13"
46+
concatenate('I', 'am', 13),
47+
"I am 13"
4448
);

week-1/2-mandatory/4-tax.js

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,28 @@ function calculateSalesTax() {}
1919

2020
function formatCurrency() {}
2121

22-
/* ======= TESTS - DO NOT MODIFY =====
22+
/* ======= TESTS - DO NOT MODIFY =====
2323
There are some Tests in this file that will help you work out if your code is working.
24-
2524
To run these tests type `node 4-tax.js` into your terminal
2625
*/
2726

28-
function test(test_name, expr) {
29-
let status;
30-
if (expr) {
31-
status = "PASSED";
32-
} else {
33-
status = "FAILED";
34-
}
27+
const util = require('util');
28+
29+
function test(test_name, actual, expected) {
30+
let status;
31+
if (actual === expected) {
32+
status = "PASSED";
33+
} else {
34+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
35+
}
3536

36-
console.log(`${test_name}: ${status}`);
37+
console.log(`${test_name}: ${status}`);
3738
}
3839

39-
test("calculateSalesTax function - case 1 works", calculateSalesTax(15) === 18);
40-
test(
41-
"calculateSalesTax function - case 2 works",
42-
calculateSalesTax(17.5) === 21
43-
);
44-
test(
45-
"calculateSalesTax function - case 3 works",
46-
calculateSalesTax(34) === 40.8
47-
);
48-
49-
test("formatCurrency function - case 1 works", formatCurrency(15) === "£18.00");
50-
test(
51-
"formatCurrency function - case 2 works",
52-
formatCurrency(17.5) === "£21.00"
53-
);
54-
test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80");
40+
test("calculateSalesTax function - case 1 works", calculateSalesTax(15), 18)
41+
test("calculateSalesTax function - case 2 works", calculateSalesTax(17.5), 21)
42+
test("calculateSalesTax function - case 3 works", calculateSalesTax(34), 40.8)
43+
44+
test("formatCurrency function - case 1 works", formatCurrency(15), "£18.00")
45+
test("formatCurrency function - case 2 works", formatCurrency(17.5), "£21.00")
46+
test("formatCurrency function - case 3 works", formatCurrency(34), "£40.80")

week-1/3-extra/1-currency-conversion.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ function convertToUSD() {}
1212
===================
1313
The business is now breaking into the Brazilian market
1414
Write a new function for converting to the Brazilian real (exchange rate is 5.7 BRL to £)
15-
They have also decided that they should add a 1% fee to all foreign transactions
16-
Find a way to add 1% to all currency conversions (think about the DRY principle)
15+
They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL.
1716
*/
1817

1918
function convertToBRL() {}
@@ -24,16 +23,18 @@ There are some Tests in this file that will help you work out if your code is wo
2423
To run these tests type `node 1-currency-conversion` into your terminal
2524
*/
2625

27-
function test(test_name, expr) {
28-
let status;
29-
if (expr) {
30-
status = "PASSED";
31-
} else {
32-
status = "FAILED";
33-
}
26+
const util = require('util');
3427

35-
console.log(`${test_name}: ${status}`);
28+
function test(test_name, actual, expected) {
29+
let status;
30+
if (actual === expected) {
31+
status = "PASSED";
32+
} else {
33+
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
34+
}
35+
36+
console.log(`${test_name}: ${status}`);
3637
}
3738

38-
test("convertToUSD function works", convertToUSD(32) === 44.8);
39-
test("convertToBRL function works", convertToBRL(30) === 172.71);
39+
test("convertToUSD function works", convertToUSD(32), 44.8);
40+
test("convertToBRL function works", convertToBRL(30), 169.29);

week-1/3-extra/2-piping.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,22 @@ There are some Tests in this file that will help you work out if your code is wo
4343
To run these tests type `node 2-piping.js` into your terminal
4444
*/
4545

46-
function test(test_name, expr) {
47-
let status;
48-
if (expr) {
49-
status = "PASSED"
50-
} else {
51-
status = "FAILED"
52-
}
53-
54-
console.log(`${test_name}: ${status}`)
46+
const util = require('util');
47+
48+
function test(test_name, actual, expected) {
49+
let status;
50+
if (actual === expected) {
51+
status = "PASSED";
52+
} else {
53+
status = `FAILED: expected: ${util.inspect(expected)} but your code returned: ${util.inspect(actual)}`;
54+
}
55+
56+
console.log(`${test_name}: ${status}`);
5557
}
5658

57-
test('add function - case 1 works', add(1,3) === 4)
58-
test('add function - case 2 works', add(2.4,5.3) === 7.7)
59-
test('multiply function works', multiply(2,3) === 6)
60-
test('format function works', format(16) === "£16")
61-
test('badCode variable correctly assigned', badCode === "£24")
62-
test('goodCode variable correctly assigned', goodCode === "£24")
59+
test('add function - case 1 works', add(1,3), 4)
60+
test('add function - case 2 works', add(2.4,5), 7.4)
61+
test('multiply function works', multiply(2,3), 6)
62+
test('format function works', format(16), "£16")
63+
test('badCode variable correctly assigned', badCode, "£24")
64+
test('goodCode variable correctly assigned', goodCode, "£24")

week-1/3-extra/3-magic-8-ball.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,27 @@ Very doubtful.
4545

4646
// This should log "The ball has shaken!"
4747
// and return the answer.
48-
function shakeBall() {}
49-
50-
// The answer should come from shaking the ball
51-
let answer;
48+
function shakeBall() {
49+
}
5250

53-
// When checking the answer, we should tell someone if the answer is
51+
// This function should say whether the answer it is given is
5452
// - very positive
5553
// - positive
5654
// - negative
5755
// - very negative
58-
function checkAnswer() {}
56+
// This function should expect to be called with any value which was returned by the shakeBall function.
57+
function checkAnswer(answer) {
58+
}
5959

60-
/* ======= TESTS - DO NOT MODIFY =====
60+
/* ======= TESTS - DO NOT MODIFY =====
6161
There are some Tests in this file that will help you work out if your code is working.
6262
6363
To run these tests type `node 3-magic-8-ball.js` into your terminal
6464
*/
6565

6666
const log = console.log;
6767
let logged;
68-
console.log = function () {
68+
console.log = function() {
6969
log(...arguments);
7070
logged = arguments[0];
7171
};
@@ -89,13 +89,25 @@ function testAll() {
8989
`shakeBall logs "The ball has shaken!"`,
9090
logged === "The ball has shaken!"
9191
);
92-
test(`shakeBall returns an string answer"`, typeof answer === "string");
92+
test(`shakeBall returns an string answer`, typeof answer === "string");
9393
test(
9494
`checkAnswer returns the level of positivity"`,
9595
["very positive", "positive", "negative", "very negative"].includes(
9696
checkAnswer(answer)
9797
)
9898
);
99+
const answers = new Set();
100+
for (let i = 0; i < 10; ++i) {
101+
answers.add(shakeBall());
102+
}
103+
test(
104+
`shakeBall returns different answers`,
105+
answers.size > 1,
106+
);
107+
test(
108+
`checkAnswer returns different answers`,
109+
new Set(Array.from(answers.values()).map(checkAnswer)).size > 1,
110+
);
99111
}
100112

101113
testAll();

0 commit comments

Comments
 (0)