Advertisement
VeroniqueVasileva

Find Average

Jan 9th, 2019
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const getGets = (arr) => {
  4.     let index = 0;
  5.  
  6.     return () => {
  7.         const toReturn = arr[index];
  8.         index += 1;
  9.         return toReturn;
  10.     };
  11. };
  12. // this is the test
  13. const test = [
  14.     '10,12,14,13'
  15.  
  16. ];
  17.  
  18. const gets = this.gets || getGets(test);
  19. const print = this.print || console.log;
  20.  
  21. //here is the solution:
  22. const input = gets().split(',').map(el => +el);
  23. print(input);
  24. const length = input.length;
  25.  
  26. let sum = 0;
  27. for (let index = 0; index < input.length; index++) {
  28.     sum += input[index];
  29. }
  30.  
  31. let average = sum / length;
  32. print(average.toFixed(2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement