LearnFrenzy
Sign in Take a free test

Array Methods · Find the Error · Question 3

Error: continue inside a callback Answer, explanation & discussion

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

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const words = ["", "js", "", "css"];
const lengths = words.map((w) => {
  if (w === "") continue;
  return w.length;
});
console.log(lengths);
  • A It prints [ 2, 3 ]
  • B SyntaxError: continue is only allowed in loops, and the callback is a function – filter first, then map Correct
  • C It prints [ undefined, 2, undefined, 3 ]
  • D It prints [ 0, 2, 0, 3 ]
Correct Answer: Option B

"Illegal continue statement". To skip items, use words.filter(Boolean).map((w) => w.length), or flatMap returning [] for empty words.

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