Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bills(input) {
- let months = Number(input[0]);
- let electricity = 0;
- let water = months * 20;
- let internet = months * 15;
- for (let i = 1; i <= months; i++) {
- electricity += Number(input[i]);
- }
- let other = (electricity + 35 * months) * 1.2;
- let average = (electricity + water + internet + other) / months;
- console.log(`Electricity: ${electricity.toFixed(2)} lv`);
- console.log(`Water: ${water.toFixed(2)} lv`);
- console.log(`Internet: ${internet.toFixed(2)} lv`);
- console.log(`Other: ${other.toFixed(2)} lv`);
- console.log(`Average: ${average.toFixed(2)} lv`);
- }
- РЕШЕНИЕ СЪС SHIFT() И WHILE:
- function bills(input) {
- let months = Number(input.shift());
- let electricity = 0;
- let water = months * 20;
- let internet = months * 15;
- while (input.length !== 0) {
- electricity += Number(input.shift());
- }
- let other = (electricity + 35 * months) * 1.2;
- let average = (electricity + water + internet + other) / months;
- console.log(`Electricity: ${electricity.toFixed(2)} lv`);
- console.log(`Water: ${water.toFixed(2)} lv`);
- console.log(`Internet: ${internet.toFixed(2)} lv`);
- console.log(`Other: ${other.toFixed(2)} lv`);
- console.log(`Average: ${average.toFixed(2)} lv`);
- }
- РЕШЕНИЕ С МЕТОДИ:
- function bills(input) {
- let months = Number(input.shift());
- let electricity = input.map(Number).reduce((a, b) => a + b, 0);
- let water = months * 20;
- let internet = months * 15;
- let other = (electricity + 35 * months) * 1.2;
- let average = (electricity + water + internet + other) / months;
- console.log(`Electricity: ${electricity.toFixed(2)} lv`);
- console.log(`Water: ${water.toFixed(2)} lv`);
- console.log(`Internet: ${internet.toFixed(2)} lv`);
- console.log(`Other: ${other.toFixed(2)} lv`);
- console.log(`Average: ${average.toFixed(2)} lv`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement