๐ง๐พโ๐ป Find the bug
Starter
Analyse
๐ป Computational Thinking
๐ค AI Thinking and AI Literacy
โฑ about 10 minutes
10 points
The challenge
This JavaScript function should add up all numbers in an array, but it returns NaN. Find the bug, explain why it happens, and give the fixed line.
function sum(nums) {
let total = 0;
for (let i = 0; i <= nums.length; i++) {
total += nums[i];
}
return total;
}
Instructions
- 1Trace what happens on the last loop.
- 2Explain the cause in one or two sentences.
- 3Write the corrected line.
Rules
- Explain in your own words โ copying an AI answer without understanding will not score the explanation criterion.
Success criteria
Your submission is marked against these criteria only.
- Gives the correct fix Checked automatically 4 pts
- Identifies the cause Checked automatically 3 pts
- Explains the NaN Checked automatically 1 pt
- Clear explanation in your own words Needs AI review โ not configured yet, so not assessed 2 pts
What an AI replied to your prompt
For comparison only โ this reply does not affect your score. Does it give you what you asked for?