LearnFrenzy
Sign in Take a free test

Strings · Find the Error · Question 8

Error: repeat with a negative count Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links