Ana Lu Spínola
Ana Lu Spínola

Reputation: 11

Array average with parameter (...rest)

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.

Upvotes: 1

Views: 2075

Answers (7)

Suz
Suz

Reputation: 87

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;
}

Upvotes: 0

Mohammed Nasif
Mohammed Nasif

Reputation: 94

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());

Upvotes: 0

Abishek
Abishek

Reputation: 1

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());

Upvotes: 0

Hari Swapna Puli
Hari Swapna Puli

Reputation: 17

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());

Upvotes: 1

EugenSunic
EugenSunic

Reputation: 13723

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());

Upvotes: 0

Djaouad
Djaouad

Reputation: 22794

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
}

Upvotes: 2

Abishek Aditya
Abishek Aditya

Reputation: 812

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;

Upvotes: 1

Related Questions