r/javaScriptStudyGroup • • Apr 30 '18

help needed calculate average with es6 rest operator

Hi everyone,

wondering if someone can assist, i have problems comprehending why my code is not working, as part of the udacity nanodegree for front-end track this exercise requires calculating the total of numbers and outputting the average however its not working as required below is my code:

// your code goes here

function average(...nums) { for(var num of nums){ // add together all numbers in the array num += num/2;

}

var total = num/2; console.log(total);

  var [value1] = nums;
  nums += value1;
  console.log(nums);
  return total;

}

// console.log(num); console.log(average(2, 6)); console.log(average(2, 3, 3, 5, 7, 10)); console.log(average(7, 1432, 12, 13, 100));

1 Upvotes

1 comment sorted by

1

u/T_O_beats May 01 '18

Without giving you the answer check out Array.reduce