# 12.I did it,though

#1

Hey guys. So i wasn't introduced to "parseInt()" and I really wonder if I am going to be introduced to it...or I could use some helpful links to learn about it... w3 did'nt do well explaining its usage...Thanks))

``````\$(document).ready(function() {
\$(document).keydown(function(key) {
switch(parseInt(key.which,10)) {
// Left arrow key pressed
case 37:
\$('img').animate({left: "-=10px"}, 'fast');
break;
// Up Arrow Pressed
case 38:
\$('img').animate({top: "-=10px"}, 'fast');
break;
// Right Arrow Pressed
case 39:
\$('img').animate({left: "+=10px"}, 'fast');
break;
// Down Arrow Pressed
case 40:
\$('img').animate({top: "+=10px"}, 'fast');
break;
}
});
});``````

#2

`parseInt()` takes a number of any base and converts it to a decimal integer. We can also convert numbers in the form of a string though the Unary operator can fill in for this role.

`````` > a = +"42"
=> 42
> a
=> 42
> typeof a
=> 'number'``````

``````   b = "42"
=> '42'
parseInt(b)
=> 42
typeof b
=> 'string'
b = parseInt(b)
=> 42
typeof b
=> 'number'``````

That's the basic function. Now lets look at numbers of different bases...

The common bases are binary, octal and hexadecimal, base 2, 8 and 16 respectively. We can pass either number or string, but the results may vary so some practice will help to demonstrate the differences.

``````parseInt(0b1000, 2)       // NaN

parseInt('0b1000', 2)     // 8

parseInt(1000, 2)         // 8

parseInt(01234, 8)        // 54  ???

parseInt(1234, 8)         // 668

parseInt("01234", 8)      // 668

parseInt(2A, 16)  // SyntaxError: identifier starts immediately after numeric literal

parseInt(0x2A, 16)        // 66    ???

parseInt('0x2A', 16)      // 42

parseInt('2A', 16)        // 42``````

The output of `parseInt()` is always a decimal integer. The second argument is the Radix, which specifies the base of the value being passed to the function.

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.