# Radix question Basic JavaScript: Use the parseInt Function with a Radix

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

A radix is a base. Like base 10 is decimal. Base 2 is binary. Base 60 is time. parseInt takes a string which may have some kind of number, and you specify what base that number will be, and it tries to get that number out of the string and convert it to an integer. If you give it a random radix rather than 2 (binary), it’s going to try to parse it to a random radix.

3 Likes

Maybe you mean that:

In the documentation:

the first sentence is:
… and returns an integer of the specified radix (the base in mathematical numeral systems).

That formulation is a bit irritating: the return value cannot be a hex (radix: 16) because you need letters a…f for a hex-representation but an integer is returned

radix is related to the input-string