I was writing the function bellow and it came to my mind to ask if there is another way to instantiate the counter variable in the loop other than using the var key word? Also if it possible in another context not in a for loop.
Obviously this code doesn't work.
function everyOther(arr) {
var sum =0;
for(i=0;i<arr.length;i+=2){
sum+=arr[i];
}
return sum;
}
This one works. Can I omit the var keyword somehow?
function everyOther(arr) {
var sum =0;
for(var i=0;i<arr.length;i+=2){
sum+=arr[i];
}
return sum;
}