Radix, parseInt and the result value

I don’t understand what the value of a parseInt() if we given a string ‘11’ and a radix of 2. This is the example given in the curriculum challenge by the way. Here it is :

Const parsed = parseInt('11',2);

The value of parsed is 3. Why 3 and not another number? How did it get the value 3 and what does it represent?
Thank you.
I feel like @snowmonkey should get into this.

@ summoning random users is somewhat strange behavior, IMHO. People will read and respond as able.

Anywho, lets look at MDN.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

It looks like the radix parameter is the ‘base’ for the number you want to parse
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#parameters

This means that this example is “base 2”. What does that mean? Well, let’s ask Wikipeda:
https://en.wikipedia.org/wiki/Binary_number

Ah, a “base 2” number is also called “binary”. It is a number with only two digits, 1 and 0.

What is the base 2 value 11 in base 10?

Well, we could read the Wikipedia article to see how to do this, or look up a freeCodeCamp article:
https://www.freecodecamp.org/news/binary-definition/

1 Like

Yeah, please don’t @ random mods, people on the forum will answer you if they’re able.

“11” is the number two in binary, anyway, somebody just asked same thing, this answer I wrote might be helpful (might not, it’s more analogy than anything else but hey ho):

1 Like

In only the last link i could understand why did we get the number 3 as a result. Everything else i found already on internet.
I guess google present diffrent results depends on diffrent countries.
You probably get better results as google knows you speak english in your country, but in here google thinks our second lang is french, so when i search in english, i dont get the best.
Thanx any way jermino.
Sorry to @ random people.

I would suggest reading through this math is fun article and watching this YouTube video on binary

That will explain how binary works.

But also looking at a visual might help.

Let’s take a look at this example I created here.

The first thing to understand are the place values.
As you can see we have a 1’s place, 2’s place, 4’s place, 8’s place, etc.
It goes all the way up to 128’s place.

Here are where the place values come from

20 equals 1
21 equals 2
22 equals 4
etc…

All of that is explained in the resources I linked.

Once you understand the place values then it is just about placing the 1’s and 0’s in the correct places.

If you want to find what 3 is in binary then you place a 1 in the 1’s place and a 1 in the 2’s place.

1+2 = 3 :grinning:

You can follow that same process for any number.

For example if you wanted to know what the number 13 is in binary.

It really boils down to adding up the appropriate values to get the number you are looking for.

Hope that helps!

For some reason when I see charts like that I get cold sweats and flashbacks to learning about subnetting when taking my CCNA course. :cold_face: :grin:

1 Like

In about 1 video suggested next to the video link you posted i got my head around binary and decimal. Thanx alot.