08-02-2023, 06:08 AM
"I've tried debugging it,"
obviously not very well
just add some console.log statements in there , like this
and you'll see the problem
obviously not very well
just add some console.log statements in there , like this
and you'll see the problem
Code:
function calculateSum(arr) {
let sum = 0;
for (let i = 0; i <= arr.length; i++) {
console.log(i + ' = ' + sum);
sum += arr[i];
}
console.log('sum = ' + sum);
return sum;
}
const numbers = [1, 2, 3, 4, 5];
const result = calculateSum(numbers);
console.log(result); // Output should be 15, but it's not