Mario


#1


https://www.codecademy.com/courses/web-beginner-en-JwhI1/2/3?curriculum_id=50a3fad8c7a770b5fd0007a1
Hey guys,

Can anyone tell me what does "parseInt(key.which,10)" do in this code?
This code works perfectly, but I don't know what does that key.which,10 do.
What I know so far is that parseInt changes String to Integer.

Cheers!


$(document).ready(function() {
    $(this).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:
				// Put our code here
				$('img').animate({top: "-=10px"}, 'fast');
				break;
			// Right Arrow Pressed
			case 39:
				// Put our code here
				$('img').animate({left: "+=10px"}, 'fast');
				break;
			// Down Arrow Pressed
			case 40:
				// Put our code here
				$('img').animate({top: "+=10px"}, 'fast');
				break;
		}
	});
});


#2

parseInt() and parseFloat() are built in functions for parsing a decimal number out of a string or a number of a different base (radix). In this case, it is telling the interpreter to expect a string that looks like a base 10 number. If it is already a number, then it is returned as is.

The key.which variable is a property in the EventObject, which is named in the parameter of the event handler, key. which will be the key code of the key pressed in that instant.

The EventObject is not preserved or persistent, but transient since it only hangs on to the latest event, which is discarded upon the next one.


#3

Cool! That's perfect thanks!


#4

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