-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShopping-Bill-Calculator.js
More file actions
52 lines (40 loc) · 1.03 KB
/
Shopping-Bill-Calculator.js
File metadata and controls
52 lines (40 loc) · 1.03 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
/**
********** Problem 4: Shopping Bill Calculator*******************
Function name: calcBill(prices, items)
Statement: Calculate total bill amount and count how many times each item appears.
Test case 1
Input:
prices = { rice: 70, oil: 180, egg: 12, sugar: 90 };
items = ["egg", "egg", "rice", "oil", "egg", "sugar"];
Output:
{
total: 352,
itemCount: { egg: 3, rice: 1, oil: 1, sugar: 1 }
}
*
*/
function calcBill(prices, items){
let total = 0;
for(let price in prices){
total+= prices[price];
}
let count = {};
for(let item of items){
if(count.hasOwnProperty(item)){
count[item]++;
}
else{
count[item] = 1;
}
}
return {
total,
count,
};
}
let output = calcBill(
{ rice: 70, oil: 180, egg: 12, sugar: 90 },
["egg", "egg", "rice", "oil", "egg", "sugar"]
);
console.log(output);
// Should print: { total: 352, itemCount: { egg: 3, rice: 1, oil: 1, sugar: 1 } }