0

Looking to receive an array and take and return the average. If it is an empty string return 0. This is the code I have so far.

For some reason I am getting NaN when entered an empty array.



function calculateAverage(numbers) {
  if ((numbers = [])) return 0;
  let total = 0;
  for (let num of numbers) {
    total += num;
  }
  let result = total / numbers.length;
  return result;
}


2
  • numbers = [] makes no sense. Why did you put it there? Commented Dec 28, 2022 at 18:57
  • if ((numbers = [])) return 0; => if (numbers.length == 0) return 0; Commented Dec 28, 2022 at 18:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.