I seem to be misunderstanding the function of the "for..of" loop. In the example below I am finding the largest sub-array in an Array (in this case [2,-1,7]). When I use the "for..of" loop it returns "NaN", but both a regular for loop "(var i = 0; i < array.length; i++)" and the "for..in" return the expected result of 8.
How does "for..of" iterate differently then a regular for loop?
var array = [2,-1,7,-7];
var largestSubArray = function (array) {
var current = 0;
var newest = 0;
for(var i of array){
newest = Math.max(0, newest + array[i]);
current = Math.max(newest, current);
}
return current;
};
console.log(largestSubArray(array));
// Returns NaN, should return 8