W2P1 done

This commit is contained in:
Andrew 2022-02-07 19:41:35 +07:00
parent c9ee8f17db
commit 7908f6eb2e
2 changed files with 51 additions and 0 deletions

38
w2/p1/app.js Normal file
View file

@ -0,0 +1,38 @@
"use strict";
const fruits = [
{ name: "apple", count: 5, price: 70 },
{ name: "orange", count: 10, price: 90 },
{ name: "pineapple", count: 15, price: 110 },
{ name: "watermelon", count: 20, price: 130 },
{ name: "cherry", count: 25, price: 150 },
{ name: "pear", count: 30, price: 170 },
{ name: "peach", count: 35, price: 190 },
];
// Сюда бы reduce...
const bill1 = {
bill: fruits,
result: (() => {
let sum = 0;
for (let i = 0; i < fruits.length; i++) {
sum += fruits[i].count*fruits[i].price;
}
return sum;
})()
}
console.log(JSON.stringify(bill1));
// Подсчёт суммы forEach'ем
const bill2 = {
bill: fruits,
result: (() => {
let sum = 0;
fruits.forEach(fruitO => sum += fruitO.count*fruitO.price);
return sum;
})()
}
console.log(JSON.stringify(bill2));
// Выведем ещё и дату
console.log(`Сегодня ${new Date().toLocaleDateString("ru-RU", {weekday: "long", year: "numeric", month: "2-digit", day: "2-digit"})}`);