1

I'm trying to write a function to calculate the elements average in an array using the parameter (...rest)

Here what I've tried:

function average(...nums) {
  let total = 0;
  
  for (const num of nums) {
    total += num;
  }
  
  return total / nums.length;
}

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

But the last test returns NaN and I don't know why.

6
  • 6
    because 0/0 equals NaN in JS. Commented Sep 26, 2019 at 19:54
  • 2
    what result do you want in this case? Commented Sep 26, 2019 at 19:54
  • you should use l'hopital rule to solve your problem Commented Sep 26, 2019 at 19:56
  • possible duplicate of stackoverflow.com/questions/36441258/… Commented Sep 26, 2019 at 19:57
  • How do I create a runnable stack snippet? Commented Sep 26, 2019 at 19:59

7 Answers 7

2

Because you're trying to divide 0 (total) by 0 (nums.length where nums is []), which is NaN in JavaScript.

You can have a check at the top of your function that returns a default value (say, 0), if the list is empty:

function average(...nums) {
    if (!nums.length) return 0;
    let total = 0;
    // rest
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are returning 0/0 (which is NaN) when you pass nothing in, as nums becomes an empty array. and its length becomes 0.

You should return nums.length ? total/nums.length : 0;

Comments

1
function average(...nums)
{
    let total = 0;  
    for(const num of nums)
    {
        total +=num;
        n= nums.length;
    }
    return total/n;
}

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

Comments

0

Check it the argument which is an array exists, if not return 0 or some value stating that average cannot be calculated.

function average(...nums) {
  if (!nums.length) {
    return 'Cannot find average'
  }
  let total = 0;
  for (const num of nums) {

    total += num;
  }
  return total / nums.length;
}

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

Comments

0

The answer what you have provided will return NAN. So to avoid this get the length of the array/set of data and then divide the total only if the length is greater then 0.

function average(...value) {
    let total =0;
    for(const argument of value) {
    total += argument;
  }
  return  value.length>0?total/value.length : total;
}

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

Comments

0

function average(...inputs) {
    let avg = 0;
    let sum = 0;
    for(const input of inputs){
        sum += input;
    }
    avg = sum / inputs.length;
    return avg;
}

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

1 Comment

Why read count for every item in the array?
0

A bit verbose, but here is a solution:

function average(...nums) {
    let total = 0;

    if (nums.length === 0) {
        return total;   
    }    
    for (const num of nums) {
        total +=num;
    }
    return total / nums.length;
}

For best practices, the last return statement should however be saved in a variable just in case the value needs to be referenced or reused elsewhere.

function average(...nums) {
    let total = 0;
    
    if (nums.length === 0) {
        return total;   
    }    
    for (const num of nums) {
        total +=num;
    }
    let averageTotal = total / nums.length;
        return averageTotal;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.