I am trying to create a function that sum all numbers in an array! I am new to JavaScript and in need of someone pointing me in the right direction!
function sum(arr) {
var i = 0;
for (var index = 0; index < arr.length; index++) {
return index += arr[i];
}
}
sum([1, 2, 3]); //6
I am trying to create a function that sum all numbers in an array! I am new to JavaScript and in need of someone pointing me in the right direction!
function sum(arr) {
var i = 0;
for (var index = 0; index < arr.length; index++) {
return index += arr[i];
}
}
sum([1, 2, 3]); //6
Share
Improve this question
edited Aug 4, 2017 at 1:13
Dan Mandel
7471 gold badge9 silver badges30 bronze badges
asked Aug 3, 2017 at 23:45
Mario GomezMario Gomez
732 silver badges7 bronze badges
1
-
2
return
exits the function, not the loop .. also, you're adding toindex
rather thani
, and usingi
as theindex
which is stcuk at0
– Jaromanda X Commented Aug 3, 2017 at 23:47
5 Answers
Reset to default 1Lots of basic issues with the code.
You need a separate variable to accumulate the result in. Your code is writing into the index variable of a for loop. This is wrong for a number of reasons.
You also return after the first iteration of the loop.
Assuming you want to use a for loop (which is not the least amount of code),
function sum(arr) {
var sum = 0;
for (var index = 0; index < arr.length; index++) {
sum += arr[index];
}
return sum;
}
Use Array.reduce() for that:
function getSum(ary){
return ary.reduce(function(sum, value) {
return sum + value;
}, 0);
}
console.log(getSum([0, 1, 2, 3]));
to illustrate where your code is wrong
function sum(arr) {
var i = 0;
for (var index = 0; index < arr.length; index++) {
return index += arr[i]; // this will return from your function in the first iteration
}
}
as the ment says, return
will exit your function in the first iteration
also, you're adding to index
, which is supposed to be the index into the array, you want to add to i
, and then return i
after the loop
so, the code should be
function sum(arr) {
var i = 0;
for (var index = 0; index < arr.length; index++) {
i += arr[index];
}
return i;
}
As another answer pointed out, a probably better alternative is to use array reduce function - however the code in that answer is not the "best" usage of reduce
function getSum(ary){
return ary.reduce(function(sum, value) {
return sum + value;
}, 0);
}
can actually be written
function getSum(ary){
return ary.reduce(function(sum, value) {
return sum + value;
});
}
This uses one less iteration, because there is no "initial value", and the first iteration adds index 0 and 1 together
Sure, it's not going to make a performance difference, but why not use built-in functions properly :p
function Sum(arr) {
var sum = 0;
for (var index = 0; index < arr.length; index++) {
sum += arr[index];
}
return index;
}
Sum([1, 2, 3]); //6
Return immediately exits a function. Your code will never sum values. What is 'i' in your code!?! And what index do!?
Here's how you get your function to work:
function sum(arr) {
// this is the variable we're going to add our numbers onto
var acc = 0;
for (var index = 0; index < arr.length; index++) {
// don't return here, return is used to return from a function
acc += arr[index];
}
// after adding all numbers, return the sum
return acc
}
sum([1, 2, 3]); //6
But there are built in ways to do this, like Array.reduce() like Scott Marcus mentioned.