Calculate average

Lee·2022년 7월 11일

Algorithm

목록 보기
44/92
post-thumbnail

❓ Calculate average

Q. Write a function which calculates the average of the numbers in a given list.

Note: Empty arrays should return 0.

✔ Solution

//#my solution
function find_average(array) {
  // your code here
  if (array.length == 0) return 0;
  let sum = array.reduce((a, b) => a + b);
  return sum / array.length;
}

//#other solution
var find_average = (array) => {  
  return array.length === 0 ? 0 : array.reduce((acc, ind)=> acc + ind, 0)/array.length
}
profile
Lee

0개의 댓글