user2970534
user2970534

Reputation: 41

Loop through range argument

I'm trying out Google Apps Script editor and a simple for loop gives me weird results.

Say I have a spreadsheet

Formula and and highlighted referenced range

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;
}

I 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

Answers (1)

user2970534
user2970534

Reputation: 41

Got it. I had to convert values to number: result += Number(array[i]);

Upvotes: 1

Related Questions