Question 8
Back to Exercise
The code below stops with an uncaught error. Which statement correctly describes the problem?
function progressBar(done, total, width = 10) {
const filled = Math.round((done / total) * width);
return "#".repeat(filled) + "-".repeat(width - filled);
}
console.log(progressBar(3, 10));
console.log(progressBar(15, 10));
- A It prints two full bars
- B It prints the first bar and an empty line
- C It prints the first bar, then RangeError: done > total makes width - filled negative, and repeat(-5) is invalid Correct
- D SyntaxError
Correct Answer: Option C
repeat needs a count from 0 to +Infinity (finite). Clamp the value: const filled = Math.min(width, Math.max(0, Math.round(…))).
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!