-
Notifications
You must be signed in to change notification settings - Fork 0
/
candystore.js
61 lines (49 loc) · 1.57 KB
/
candystore.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
51
52
53
54
55
56
57
58
59
60
61
const boughtCandyPrices = []; // Array to store candy prices
function addCandy(candyType, weight) {
let pricePerGram = 0;
// Determine price per gram based on candy type
switch (candyType) {
case 'sweet':
pricePerGram = 0.5;
break;
case 'chocolate':
pricePerGram = 0.7;
break;
case 'toffee':
pricePerGram = 1.1;
break;
case 'chewing-gum':
pricePerGram = 0.03;
break;
default:
console.log(`Invalid candy type: ${candyType}`);
return; // Exit function if candy type is invalid
}
// Calculate total price of this candy
const totalPrice = pricePerGram * weight;
// Add the price to the array
boughtCandyPrices.push(totalPrice);
console.log(`Added ${totalPrice} to boughtCandyPrices`);
}
function canBuyMoreCandy() {
const amountToSpend = Math.random() * 100; // Random amount to spend (0 to 100)
let totalSpent = 0;
// Calculate total spent from boughtCandyPrices array
for (let price of boughtCandyPrices) {
totalSpent += price;
}
// Check if remaining amount is enough to buy more candy
if (totalSpent < amountToSpend) {
console.log("You can buy more, so please do!");
return true;
} else {
console.log("Enough candy for you!");
return false;
}
}
// Add some candies
addCandy("sweet", 20); // Adds the price of 20 grams of sweets
addCandy("chocolate", 15); // Adds the price of 15 grams of chocolate
addCandy("toffee", 30); // Adds the price of 30 grams of toffee
// Check if we can buy more candy
canBuyMoreCandy();