user6942759
user6942759

Reputation:

Trying to create a Javascript function that sums up numbers

I'm super new to Javascript and I'm trying to write a function that sums up numbers. The function below returns "0" instead of the sum (and I have no idea why).

function sumTo(N) {
  var total = 0
  for (var i = 0; i < N.length; i++) {
  total = total + N[i];
 }
 return total;
 };
 console.log(sumTo(1,2,3,4,5));

Upvotes: 0

Views: 607

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1075567

You're trying to get length from a number (the first number you pass to your function; 1 in your example). Numbers don't have a length, so you get undefined, and 0 < undefined is false, so your loop never runs.

Either:

  1. Pass an array into sumTo

    console.log(sumTo([1, 2, 3, 4, 5]);
    // Note ----------^-------------^
    

    or

  2. Use the arguments automatic local variable

    for (var i = 0; i < arguments.length ++i) {
        total += arguments[i];
    }
    

    or

  3. Use ES2015's "rest" arguments:

    // ES2015+ only!
    function sumTo(...values) {
       let total = 0;
       for (let i = 0; i < values.length; ++i) {
           total += values[i];
       }
       return total;
    }
    

    Since you're in ES2015 at that point, you could use reduce with an arrow function (sums are a perfect use case for reduce):

    // ES2015+ only!
    function sumTo(...values) {
       let total = values.reduce((a, b) => a + b);
       return total;
    }
    

    Or for-of, another ES2015 thing:

    // ES2015+ only!
    function sumTo(...values) {
       let total = 0;
       for (let value of total) {
           total += value;
       }
       return total;
    }
    

Upvotes: 5

Nina Scholz
Nina Scholz

Reputation: 386848

You need either an array for the numbers, or use the arguments object.

function sumTo(N) {
    var total = 0
    for (var i = 0; i < N.length; i++) {
        total = total + N[i];
    }
    return total;
}

console.log(sumTo([1, 2, 3, 4, 5]));

With arguments

function sumTo(N) {
    var total = 0
    for (var i = 0; i < arguments.length; i++) {
        total = total + arguments[i];
    }
    return total;
}

console.log(sumTo(1, 2, 3, 4, 5));

With ES6, you could use the rest parameter ....

function sumTo(...N) {
    var total = 0
    for (var i = 0; i < N.length; i++) {
        total = total + N[i];
    }
    return total;
}

console.log(sumTo(1, 2, 3, 4, 5));

Upvotes: 2

Rajesh
Rajesh

Reputation: 24955

As commented, you should send your values in an array:

function sumTo(N) {
  var total = 0
  for (var i = 0; i < N.length; i++) {
    total = total + N[i];
  }
  return total;
};
console.log(sumTo([1, 2, 3, 4, 5]));

As an alternate, you can use Array.reduce

function sumTo(N) {
  return N.reduce(function(p,c){
    return p+c
  })
};
console.log(sumTo([1, 2, 3, 4, 5]));

Upvotes: 1

kevin ternet
kevin ternet

Reputation: 4612

You could also use reduce method :

var sumTo = ar => ar.reduce((a,b) => a+b);
console.log(sumTo([1, 2, 3, 4, 5])); // 15

Upvotes: 1

Joseph
Joseph

Reputation: 119877

That's because N is just the first argument, which in this case is 1. 1 has no index, thus N[1] does not work.

What you need is to pass in an array, since you're expecting N to have length.

console.log(sumTo([1, 2, 3, 4, 5]));

Upvotes: 2

Related Questions