-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path20. Q20.js
More file actions
48 lines (43 loc) · 1.33 KB
/
20. Q20.js
File metadata and controls
48 lines (43 loc) · 1.33 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
// 20. In the following shopping cart add, remove, edit items
// => const shoppingCart = ['Milk', 'Coffee', 'Tea', 'Honey']
// - add 'Meat' in the beginning of your shopping cart if it has not been already added
// - add Sugar at the end of your shopping cart if it has not been already added
// - remove 'Honey'
// - modify Tea to 'Green Tea'
const shoppingCart = ["Milk", "Coffee", "Tea", "Honey"];
// add 'Meat' in the beginning of your shopping cart if it has not been already added
let addMeat = () => {
if (shoppingCart.includes("Meat")) {
console.log("Meat is already added");
return;
} else {
shoppingCart.unshift("Meat");
console.log(shoppingCart);
}
};
addMeat();
// add Sugar at the end of your shopping cart if it has not been already added
let addSugar = () => {
if (shoppingCart.includes("Sugar")) {
console.log("Sugar is already added");
return;
} else {
shoppingCart.push("Sugar");
console.log(shoppingCart);
}
};
addSugar();
// remove 'Honey'
let removeHoney = () => {
let index = shoppingCart.indexOf("Honey");
shoppingCart.splice(index, 1);
console.log(shoppingCart);
};
removeHoney();
// modify Tea to 'Green Tea'
let modifyTea = () => {
let index = shoppingCart.indexOf("Tea");
shoppingCart.splice(index, 1, "Green Tea");
console.log(shoppingCart);
};
modifyTea();