"Help wanted for Events 2 skill test"

Hello,

Would someone please shed some light on what I am doing wrong with the code below as a solution for Events 2 skill test (https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Events):

document.addEventListener('keydown', moveBall);

function moveBall() {

    if ('key' == 'w') {

        drawCircle(x, y + 10, size);

    } else if ('key' == 's') {    

        drawCircle(x, y - 10, size);

    } else if ('key' == 'a') {

        drawCircle(x - 10, y, size);

    } else if ('key' == 'd') {

        drawCircle(x + 10, y, size);

    } else {

        drawCircle(x, y, size);

    } 

}

Thank you.

Hello @ciromello

you need to forward the event to your function

so change this

function moveBall() {

to this

function moveBall(e) {

then use the e.code to get the pressed key

check this one

you can use the console.log(e.code) to print the output of the pressed key to know which value you need to replace with w s and so on

it recommended to use === for comparison as == compare 2 value despite if they of same type or not
for example a = “55” and b =55 is we use a==b it will be true even a is string and b is integer

hope that help and have a nice day :slight_smile:

Thank you so much.

Does this look correct?

    document.addEventListener('keydown', event => {

    if (event.keyCode === 87) {

        y -= 10;

    } else if (event.keyCode === 83 ) {    

        y += 10;

    } else if (event.keyCode === 65) {

        x -= 10;

    } else if (event.keyCode === 68) {

        x += 10;

    } else {

        x = x

    } 

drawCircle(x, y, size);

}

);

you very welcome
first one is fine but you need to replace all those == with === and ‘key’ to e.key

hope that help and have a nice day