Skip to content
Advertisement

Why is this first line not a function? [closed]

Getting an error that this is not a arr is not a function but I can’t understand why

var sumArray = function(arr) {
    total = 0;
    for (i = 0; i < arr.length; i++){
        total =+ arr(i);
        return total;
    };
};

var arr = [1, 2, 3];

Advertisement

Answer

arr is an array, so you need to indicate each element like array[i]

var sumArray = function(arr) {
    var total = 0; //<---- missing `var` keyword
    for (var i = 0; i < arr.length; i++){ //<---- missing `var` keyword
        total += arr[i]; //<---- indicate arr[i], OP update from =+ to +=
    };
    return total; //<----- should be return after for loop finish.
};

var arr = [1, 2, 3];
console.log(sumArray(arr))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement