-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise-11.js
50 lines (49 loc) · 1.49 KB
/
exercise-11.js
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
//Logic Challenge - Shopping Time!
function shoppingTime(memberId, money) {
var detail = {
memberId,
money,
listPurchased: [],
changeMoney:money
};
var item = {
"Sepatu Stacattu": 1500000,
"Baju Zoro": 500000,
"Baju H&N": 250000,
"Sweater Uniklooh": 175000,
"Casing Handphone": 50000
}
if (memberId === '' || money === undefined || memberId === undefined) {
return 'Mohon maaf, toko X hanya berlaku untuk member saja'
}
if (money < 50000) {
return 'Mohon maaf, uang tidak cukup'
}
for (let property in item) {
if (item[property] <= detail.changeMoney) {
detail.listPurchased.push(property);
detail.changeMoney -= item[property];
}
}
return detail;
}
// TEST CASES
console.log(shoppingTime('1820RzKrnWn08', 2475000));
//{ memberId: '1820RzKrnWn08',
// money: 2475000,
// listPurchased:
// [ 'Sepatu Stacattu',
// 'Baju Zoro',
// 'Baju H&N',
// 'Sweater Uniklooh',
// 'Casing Handphone' ],
// changeMoney: 0 }
console.log(shoppingTime('82Ku8Ma742', 170000));
//{ memberId: '82Ku8Ma742',
// money: 170000,
// listPurchased:
// [ 'Casing Handphone' ],
// changeMoney: 120000 }
console.log(shoppingTime('', 2475000)); //Mohon maaf, toko X hanya berlaku untuk member saja
console.log(shoppingTime('234JdhweRxa53', 15000)); //Mohon maaf, uang tidak cukup
console.log(shoppingTime()); ////Mohon maaf, toko X hanya berlaku untuk member saja