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.
In mathematical numeral systems, the radix or base is the number of unique digits, including the digit zero, used to represent numbers in a positional numeral system. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 through 9.
In any standard positional numeral system, a number is conventionally written as (x)y with x as the string of digits and y as its base, although for base ten the subscript is usually assumed (...
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
camper
January 6, 2019, 1:14pm
4
@mandeep9362 , I wrote about radix previously:
Observation about Radix ( parseInt
)
For Basic JavaScript: Use the parseInt Function with a Radix , I was not familiar with the term radix
, so I did a little more research so I can fully understand the parseInt
function.
According to MDN’s parseInt() page , one should always specify a radix, usually 10
:
radix
An integer between 2
and 36
that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10
for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10
.
So, what’s radix
and why would we want to use anything other than 10
?
According to Wikipedia’s Radix entry :
In mathematical numeral systems, the radix or base is the number of unique digits, including zero, used to represent numbers in a positional numeral system. For example, for the decimal system (the most common system in use today) the radix is ten, because it uses the ten digits from 0 through 9.
Turns out, according to Wikipedia’s Radix entry’s “In numeral systems” section , most of us in web development are familiar with the hexadecimal system (radix number 16). Most of us are also used to using other systems in some form, like the binary numeral system or base two (radix number 2) because computers are based on this (ones and zeros) and the sexagesimal system (radix number 60) when we look at our clocks or navigate on a map.
So, parseInt
and radix are nice to know since as web developers, we may need to convert times, navigation coordinates, or colors from hex to rgb ( parseInt("FF", 16) \\ 255
). We may also want to enable users to enter numbers as strings, like one month
for example, to make our inputs more user friendly and natural. Then, we can convert that one
to 1
using parseInt("one", 10) \\ 1
so we can do more things with it in our code.
Maybe that will help.
4 Likes
Thanks to all campers, now i got what it was saying
1 Like