JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – array map converts an element value with parseInt

JavaScript Quiz – array map converts an element value with parseInt

Array Map Method

Javascript map array element is used to modify the array element based on the condition pass to it. Array map method creates a new array populated with modified array.

Array map method run a provided function for each element.

Basic Syntax of Array map method

let array = arr.map(function callback( currentElem[, index[, array]]) {
    // return element for new_array
}[, thisArg])

For more detail about the javascript array map method click here.

Javascript parseInt Function

In javascript, parseInt function help to convert string number to number whose data type is number.

Basic Syntax of parseInt function

The parseInt() function parses a string and returns an integer.

parseInt()

For more detail about parseInt() click here.

JavaScript Question

In this javascript quiz, find out does array map converts an element value with parseInt as map modify the array elements based on condition.

First, we declare a variable array using the const keyword and assign a javascript array to an array variable with an array map method and parseInt function pass to it.

const array = [10, 20, 30].map(parseInt);

Now, we are checking the value of array variable using console log.

console.log(array);

JavaScript Quiz Test

const array = [10, 20, 30].map(parseInt);

console.log(array); // output => ?

Answer – [10, NaN, NaN]

Click To Run Code

Explanation

Now, you know the answer is [10, NaN, NaN] But why doesn’t it give the [10, 20, 30] because already they are number to a string. Let me explain

So, all this happen because of radix, as map required three field and parseInt take two parameters.

And second parameter is belong to radix which is by default 10.

But when it pass to maps so during first iterration.

parseInt(10, 0, [10, 20, 30]);   => 10

Since 0 is falsy, the radix is set to the default value 10. But when index value increase then.

// Second iteration: val = 20, index = 1, array = [10, 20, 30]
parseInt(20, 1, [10, 20, 30]);   => NaN

In a radix 1 system, the symbol '10' does not exist. As with the first iteration, the last argument is ignored. So, parseInt() returns NaN.

At last, That’s why we get an answer “[10, NaN, NaN]“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – can array really be access with an array
  2. JS Quiz – what really is the array index value
  3. JS Quiz – does object hasOwnProperty really return boolean
  4. JavaScript Quiz – does class typeof operator give class

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link