Use the parseInt Function with a Radix explanation needed!

Tell us what’s happening:

Your code so far


function convertToInteger(str) {

var radix = Math.floor(Math.random() * 36 - 2 + 1) + 2;

return(parseInt(str,2)); //The radix can be an integer between 2 and 36. when i do it radix at the place of 2 it fails ???

}

convertToInteger("10011");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-the-parseint-function-with-a-radix

Hey, i know its a already fixed solution, but i don’t understand, why i should use math.random () and math.floor() and i would like some kind of explanation, i feel i need to understand this to advance more further. i know what these function does, but the example didnt even mention math functions, and i feel very confused :stuck_out_tongue:

Where does it say that you should use Math functions?

it doesnt at all, but when i looked in the forum a guy solved the same problem using math functions, thats why i feel very confused and want a explanation to understand it further, i want to truly understand this before i move further ^^

And where is the link to the post?

here ya go
but as u stated it doesnt mention math functions, and thats why i belive there is a other solution if u bump me in the right direction. i dont want the answer. i want to learn this.

but is it neccesary using math functions?

He didn’t solve anything. Read that thread more carefully.

2 Likes

Based on this ^

radix parameter == An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. The description below explains in more details what happens when this argument is not provided.

What that user was trying to do was randomly generate a number between 2 and 36.
So that his output can randomly become either binary, base 3, base 4…decimal…

Unsure why, since I can’t think of any real world use cases of this.

This is a edge case of a student playing with tools, and not a new way to do things.

1 Like

Yes, ignore that random crap and you don’t need any Math functions for this.

You only need to add one line to the function to get this to pass. You need to convert this base 2 string to a number and return it. Since you know the radix is 2 (binary number) you can hard code it. They even give you an example:

The function call looks like:

parseInt(string, radix);

And here's an example:

var a = parseInt("11", 2);

That second example is basically what you want in your function, but instead of assigning it to a variable, you need to return it. And you need to use the string that is passed into the function. This is one line of code.

You are not alone. A lot of people get confused on this because they forgot how the place-value number system and radices and different number bases and such work. But you’re making this more complicated than it needs to be.

1 Like

Yes, the thing is i got stuck so i watched the forum for help, and still didnt understand, but now its more clearly actually and i passed it :smiley: Thanks for the help though!

Yeah it actually got me more confused :stuck_out_tongue: but Thank you for input, i passed the test, but i will try diffrent examples of parseInt, too get more confident on the topic !

For people not understanding how parseInt works, the problem is usually not understanding how number bases (radices) work. We were all taught it in school, but unless you think number theory is fun, most people forget it. Historically it was very important in computer programming. It’s still kind of important but doesn’t come up as often.

This would probably be a case where youtube would be very useful because this is easier to explain visually for a lot of people. Just check on youtube for “number base system” or “binary numbers”. I’m sure there are a lot of helpful videos on the subject.

1 Like

Will check it out, it seems like a good knowledge to have!

thanks once again!