Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

this code make me crazy

plz help me to understand this code

$(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:
                // Put our code here
                break;
            // Right Arrow Pressed
            case 39:
                // Put our code here
                break;
            // Down Arrow Pressed
            case 40:
                // Put our code here
                break;
        }
    });
});

1-keydown(function(key) why he put key in function what this key do

switch(parseInt(key.which,10))

10? key.which parseInt and the code

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,696 Points

Formatted your code. From the Markdown Cheatsheet:

Wrap your code with 3 backticks (```) on the line before and after. If you specify the language after the first set of backticks, that'll help us with syntax highlighting.

        ```html
        <p>This is code!</p>
        ```

1 Answer

1-. He put key as parameter in that function because is a callback function, google that, so when the document is ready this means that page has load completely excute this function, saying $(document).keydown(function(key){}) this means listen to any key and excute this function and passing the parameter key you can use it to see what key was pressed