Reputation: 41
I'm trying out Google Apps Script editor and a simple for loop gives me weird results.
Say I have a spreadsheet
In script editor I write a function:
function calculate(array) {
var result = 0;
for (var i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
array.length
I get 2 as expectedarray[0]
I get 4 as expectedI tried googling it but i found some weird workarounds using array.map
function.
Am I doing something wrong, is it not supposed to be plain javascript?
Upvotes: 0
Views: 443
Reputation: 41
Got it. I had to convert values to number: result += Number(array[i]);
Upvotes: 1