LearnFrenzy
Sign in Take a free test

Array Methods · Find the Error · Question 11

Bug: quantities become NaN Answer, explanation & discussion

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

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const qtyInputs = ["5", "12", "8"];          // values from form fields
const qtys = qtyInputs.map(parseInt);
console.log(qtys);
  • A parseInt cannot read form values
  • B The array must be converted with Array.from
  • C map passes the index as parseInt's radix – use qtyInputs.map(Number) or (s) => parseInt(s, 10) Correct
  • D map skips the first element
Correct Answer: Option C

parseInt("12", 1) and parseInt("8", 2) are NaN because 1 is an invalid radix and 8 is not a binary digit.

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