forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex6-totalCost.js
More file actions
37 lines (31 loc) · 806 Bytes
/
ex6-totalCost.js
File metadata and controls
37 lines (31 loc) · 806 Bytes
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
const cartForParty = {
chips: 1.75,
juice: 2.4,
frenchfries: 4.99,
cake: 8.99,
choclate: 2.99,
};
function calculateTotalPrice(obj) {
// let sum = 0;
// for (const [_, value] of Object.entries(obj)) {
// sum += value;
// }
// return Number(sum.toFixed(2));
const total = Object.values(obj).reduce((total, item) => total + item);
return Number(total.toFixed(2));
}
function test1() {
console.log('\nTest 1: calculateTotalPrice should take one parameter');
console.assert(calculateTotalPrice.length === 1);
}
function test2() {
console.log('\nTest 2: return correct output when passed cartForParty');
const expected = 21.12;
const actual = calculateTotalPrice(cartForParty);
console.assert(actual === expected);
}
function test() {
test1();
test2();
}
test();