Use the parseint function with a radix

Not understanding how to solve this problem…I am lost.

Your code so far



function convertToInteger(str, radix) {
  var binary = [[128],[64],[32],[16],[8],[4],[2],[1]];
  
  for(var x = 0; x < binary.length; x++){
    
  }
convertToInteger("10011");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:

You are trying to solve this manually. JavaScript already has a function to do it for you.

parseInt(numStr, radix)

The numStr is the string of the number you want converted, and the radix is the base you want. In the function you are supposed to build, you are passed the number (string) and you are to assume that the radix is 2. You just need to return the result of that. It should just be one line. I would right it out, but that would give it away.

Don’t calculate the number, let parseInt do it for you.

If that isn’t clear, please ask for more clarification.

2 Likes

this is the way I did it at first but its not passing all the test.

function convertToInteger(str, radix) {
return parseInt(str, radix);
}

convertToInteger(“10011”, 2);

1 Like

Thanks, this passed the test so I believe I got it right/

function convertToInteger(str) {
var radix = 2;
return parseInt(str, radix);
}

convertToInteger(“10011”);

5 Likes

This way also worked:

function convertToInteger(str) {

return parseInt(str, radix = 2);
}

convertToInteger(“10011”);

return parseInt(str, radix = 2);

That is a bit squirely.

You’re previous one:

function convertToInteger(str) {
  var radix = 2;
  return parseInt(str, radix);
}

… works really well if you want to be very clear.

But most people would just do:

function convertToInteger(str) {
  return parseInt(str, 2);
}
12 Likes

I want to learn the correct way so I was hoping to get a answer like that. Thanks

3 Likes

@mmorgan86 and @kevinSmith - I understand that this solved but I have a semi-related question here…

Why was “2” chosen for radix (and passed) but another number like “36” doesn’t pass at all? Also, when I tried “5,” it passed all but 2 tests. I was playing around with the numbers between 2 and 36 to see if it would pass no matter which number was in this particular challenge. I guess I just don’t understand the logic of the entire challenge. :confused:

3 Likes

Because the question specifically involved parsing binary, or base 2, numbers. If you remember from school, we use base 10 in our every day life. “Radix” is another word for “base”.

Please let us know if this doesn’t clear up your question.

9 Likes

@kevinSmith That makes sense now! I suppose my brain just overlooked it. The introduction in the wiki link helped. For whatever reason, I wasn’t thinking about base at all much less base 2. I think I was most focused on “The radix can be an integer between 2 and 36” in the challenge.

Thanks for taking the time to explain it. :slight_smile:

2 Likes

Thank You I understand alot more.
In school I only had basic math (ONLY base 10) so I was lost with radix.
;-D
Jan

Why does the first solution equal 19? when passed through the function

1 Like

Because it the binary number is 10011, those represent the place values of (going backwards, right to left) 1, 2, 4, 8, 16.

We have:

1X1
1X2
0X4
0X8
1X16

Those add together to 19. If this still doesn’t make sense. Look up some youtube videos on how binary numbers work.

2 Likes
1 Like

Gotta love when Radix isn’t explained at all and then they want you to do this.

I got all the code right besides filling in a “2” and I still have no idea why it’s there.

1 Like

parseInt() convert a number to your normal way of counting (base 10), you need to tell it in which base the string you use is in, and as that is in binary the base (the radix) is 2. You are using the parseInt() function to convert a string str that is written in binary radix=2 to the decimal system

If the string was in a ternary counting system you would have written radix=3.

Does this explain it?

1 Like

FCC isn’t meant to be comprehensive. Not every little thing gets explained. Things like radices are more of a math concept. Many of remember how they work from from school. It’s cool if you don’t (we all run into some things that we don’t remember.) FCC expects you to go out and figure out the things you don’t understand. Reread the lesson and if you can’t find it, google it. And if that doesn’t help, check here.

1 Like

I looked it all up, binary and all. I still would argue that it needs to be explained.

1 Like

If FCC explained every little thing that everyone could possibly not know, it would balloon to 100x its size. And a lot of people would get bored. Radix/base/binary is basic math. It’s cool that some people don’t remember, but FCC is not in the business of teaching math, basic or otherwise. Probably most of the people who come here are pretty tech/science/math savvy so it’s reasonable to assume that most people won’t need that (again, basic math, not programming) explained. Again, I’m not putting down people that don’t remember that from school - there are plenty of things I don’t remember. I don’t think there is a need for FCC to duplicate information that is already readily available elsewhere, especially when it is math and not programming. (Decades ago, understanding binary was fundamental to programming, but in a high level language like JS, you hardly ever need it. I can’t remember needing it once in JS.)

I would also point out that knowing how to go out and find things on your own is fundamental to programming. It is impossible to teach you everything you know about programming. And by the time you get a job, 10% of it will be obsolete anyway. For example, when I started FCC, ES6 still wasn’t expected, and React has gone through some radical changes.

As a professional developer, I go to the docs or google or stack overflow at least once a day. On some days, if I’m working with a new library, it can be several times an hour. My boss doesn’t provide tutorials. Developers are expected to just know how to tell what they need to figure it out, know how to figure it out, and go and do it. I think it’s a good thing that FCC doesn’t hold your hand and spoon feed you everything.

But FCC is a volunteer organization. If you really, really think FCC needs to teach this, then you can go to the git repo and create and issue. If you really want to be a hero, you can edit that problem or create a new section on basic math and do a PR. But it is a volunteer organization that works hard and I think has done a pretty good job of delivering fairly good product for free.

6 Likes

This helped me tremendously!! Thank you!!