Spocoman

05. Average Number

Dec 28th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function averageNumber(input) {
  2.     let num = Number(input[0]);
  3.     let index = 1;
  4.     let sum = 0;
  5.  
  6.     while (index <= num) {
  7.         sum += Number(input[index++]);
  8.     }
  9.     console.log(`${(sum / num).toFixed(2)}`);
  10. }
  11.  
  12. РЕШЕНИЕ С FOR:
  13.  
  14. function averageNumber(input) {
  15.     let num = Number(input[0]);
  16.     let sum = 0;
  17.  
  18.     for (let i = 1; i <= num; i++) {
  19.         let number = Number(input[i]);
  20.         sum += number;
  21.     }
  22.     console.log(`${(sum / num).toFixed(2)}`);
  23. }
  24.  
Add Comment
Please, Sign In to add comment