Use the parseint function with a radix

Use the parseint function with a radix
0.0 0

#1

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:


#2

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.


#3

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);


#4

Your are missing something important in the instructions.

Use parseInt() in the convertToInteger function so it converts a binary number to an integer and returns it.

If the function is called without passing a second argument (for radix), you still assume the first argument is a binary number and use parseInt with the applicable radix value for binary numbers.


#5

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

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

convertToInteger(“10011”);


#6

This way also worked:

function convertToInteger(str) {

return parseInt(str, radix = 2);
}

convertToInteger(“10011”);


#7
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);
}

#8

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


#9

@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:


#10

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.


#11

@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: