forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex4-shoppingCart.js
More file actions
57 lines (49 loc) · 1.46 KB
/
ex4-shoppingCart.js
File metadata and controls
57 lines (49 loc) · 1.46 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
const shoppingCart = ['bananas', 'milk'];
function addToShoppingCart(item) {
if (item !== undefined) {
shoppingCart.push(item);
if (shoppingCart.length > 3) {
shoppingCart.shift();
}
}
return `You bought ${shoppingCart.join(', ')}!`;
}
// ===== manual tests (restore) =====
function test1() {
console.log('Test 1: add `chocolate` to the cart');
const expected = 'You bought bananas, milk, chocolate!';
const actual = addToShoppingCart('chocolate');
console.assert(actual === expected);
}
function test2() {
console.log('Test 2: add `waffles` (keep last 3 items)');
const expected = 'You bought milk, chocolate, waffles!';
const actual = addToShoppingCart('waffles');
console.assert(actual === expected);
}
function test3() {
console.log('Test 3: add `tea` (bananas removed)');
const expected = 'You bought chocolate, waffles, tea!';
const actual = addToShoppingCart('tea');
console.assert(actual === expected);
}
function test4() {
console.log('Test 4: add nothing (cart unchanged)');
const expected = 'You bought chocolate, waffles, tea!';
const actual = addToShoppingCart();
console.assert(actual === expected);
}
function test5() {
console.log('Test 5: `tea` should be added and `milk` removed');
const expected = 'You bought chocolate, waffles, tea!';
const actual = addToShoppingCart('tea');
console.assert(actual === expected);
}
function test() {
test1();
test2();
test3();
test4();
test5();
}
test();