Advertisement
Spocoman

06. Bills

Dec 26th, 2021 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function bills(input) {
  2.     let months = Number(input[0]);
  3.     let electricity = 0;
  4.     let water = months * 20;
  5.     let internet = months * 15;
  6.  
  7.     for (let i = 1; i <= months; i++) {
  8.         electricity += Number(input[i]);
  9.     }
  10.  
  11.     let other = (electricity + 35 * months) * 1.2;
  12.     let average = (electricity + water + internet + other) / months;
  13.  
  14.     console.log(`Electricity: ${electricity.toFixed(2)} lv`);
  15.     console.log(`Water: ${water.toFixed(2)} lv`);
  16.     console.log(`Internet: ${internet.toFixed(2)} lv`);
  17.     console.log(`Other: ${other.toFixed(2)} lv`);
  18.     console.log(`Average: ${average.toFixed(2)} lv`);
  19. }
  20.  
  21. РЕШЕНИЕ СЪС SHIFT() И WHILE:
  22.  
  23. function bills(input) {
  24.     let months = Number(input.shift());
  25.     let electricity = 0;
  26.     let water = months * 20;
  27.     let internet = months * 15;
  28.  
  29.     while (input.length !== 0) {
  30.         electricity += Number(input.shift());
  31.     }
  32.  
  33.     let other = (electricity + 35 * months) * 1.2;
  34.     let average = (electricity + water + internet + other) / months;
  35.    
  36.     console.log(`Electricity: ${electricity.toFixed(2)} lv`);
  37.     console.log(`Water: ${water.toFixed(2)} lv`);
  38.     console.log(`Internet: ${internet.toFixed(2)} lv`);
  39.     console.log(`Other: ${other.toFixed(2)} lv`);
  40.     console.log(`Average: ${average.toFixed(2)} lv`);
  41. }
  42.  
  43. РЕШЕНИЕ С МЕТОДИ:
  44.  
  45. function bills(input) {
  46.     let months = Number(input.shift());
  47.     let electricity = input.map(Number).reduce((a, b) => a + b, 0);
  48.     let water = months * 20;
  49.     let internet = months * 15;
  50.     let other = (electricity + 35 * months) * 1.2;
  51.     let average = (electricity + water + internet + other) / months;
  52.  
  53.     console.log(`Electricity: ${electricity.toFixed(2)} lv`);
  54.     console.log(`Water: ${water.toFixed(2)} lv`);
  55.     console.log(`Internet: ${internet.toFixed(2)} lv`);
  56.     console.log(`Other: ${other.toFixed(2)} lv`);
  57.     console.log(`Average: ${average.toFixed(2)} lv`);
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement