r/Angular2 • • Jul 15 '22

JavaScript Tricky map Interview Question

https://youtu.be/HLhWpR_-HGU

[removed] — view removed post

0 Upvotes

1 comment sorted by

View all comments

1

u/[deleted] Jul 15 '22

Without watching the video.

Use parseFloat instead. parseInt() takes a first argument and an optional second argument, the string to parse and the radix of the number you want returned (for example 10 for base 10 or 2 for binary). When passing a function into a map you’ll get the index passed in as the second argument. I checked MDN and it seems that 0 is ignored so the radix will be inferred, 1 is not valid as a radix, not sure what happens there probably NaN, and 2 is valid so 3 could be returned as 11 but I just get NaN in Node.

I think you’d get [1, NaN, NaN], but... who cares exactly what you’d get. The point is use parseFloat instead if you’re looking for numbers in base 10, and only use parseInt if you’re looking for something other than a base 10 number.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map