-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion-2-js.js
More file actions
36 lines (28 loc) · 1.12 KB
/
Question-2-js.js
File metadata and controls
36 lines (28 loc) · 1.12 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
const item = [ {name: 'Bike', price:100}, {name: 'TV', price:200}, {name: 'Album', price:10},
{name: 'Book', price:5}, {name: 'Phone', price:500}, {name: 'Computer', price:1000}
]
const sorted = item.sort((a, b) => {
return a.price - b.price
});
let lowPrice = sorted[0]
let highPrice = sorted[item.length - 1]
const total = (arr) => {
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i].price;
}
return total;
}
const totalExcept = (arr) => {
let total = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i].price >= 10) {
total += arr[i].price;
}
}
return total;
}
console.log("1.the cheapest product in the array: " + lowPrice.name + ", with the price of: " + lowPrice.price + " dollars");
console.log("2.the expensive product in the array: " + highPrice.name + ", with the price of: " + highPrice.price + " dollars");
console.log("3.Total price of all products combined: " + total(item) + " dollars");
console.log("4. Total price of all products combined, except products under 10 dollar: " + totalExcept(item) + " dollars");