Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
parseInt is giving NaN when not using arrow function

Why does parseInt give NaN when I am not putting it in an arrow function? What's strange is that only 50 is getting properly parsed, while the others are not... Is there something wrong with putting parseInt itself as an argument? I want to make sure all of them are whole numbers, it works with an arrow function but I would like to know why it doesn't work just passing in the whole function. Even trying to bind it to window with .map(parseInt.bind(window)) doesn't work. Is it a problem with the source code?

function toPercent(array) {
    let arr = array.map((a, _, s) => a / s.reduce((a, b) => a+b) * 100)
    console.log(arr)
    return arr.map(parseInt)
}

console.log(toPercent([2, 1, 1]))

function toPercent(array) {
        let arr = array.map((a, _, s) => a / s.reduce((a, b) => a+b) * 100)
        console.log(arr)
        return arr.map(n => parseInt(n))
    }

    console.log(toPercent([2, 1, 1]))

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The callback to map is called with multiple parameters (element, index, array), and parseInt takes multiple parameters (string, radix) so the index is being passed as the radix. That's probably not what you intended.

The NaNs arise because the radix must be between 2 and 36. So you'll get some strange results radix is 1 and other indices will attempt to parse according to the ever-incrementing radix. By even further quirkiness, radix 0 means to use base 10 (unless the string starts with 0x or 0X, in which case it means to use base 16 ignoring the prefix in the expected way). parseInt returns NaN when parsing fails.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!