0
I started at Ruby the other day and I have a question. I was doing the Hackerrank exercises and in a certain exercise it was necessary to do the sum of the array elements, not simple? Yes. But my ruby code had an error message:
def sum(n, arr)
sum = 0
for i in (0..n) do
sum += arr[i]
end
return sum
end
Error:
Solution.Rb:6:in
+': nil can't be coerced into Integer (TypeError) from solution.rb:6:in
block in simpleArraySum' from Solution.Rb:5:ineach' from solution.rb:5:in
simpleArraySum' from Solution.Rb:15:in `'
Javascript equivalent code:
const sum = function (n, arr) {
let sum = 0;
for (let i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
};