I finished Codeacademy and I'm looking to practice and get better at JavaScript. Is this coded correctly or should I have made a function for it somehow? The purpose of this code is to move a square around the page with arrow keys or buttons.
I have the entire script hosted here.
I mostly want to "Simplify this chain of if statements" but also the post about using a map instead of the if statements is something I needed to know.
function anim(e){
if((e.keyCode === 37)||(e === 37)){
y = shipLeft;
shipLeft -= 11;
y -= 11;
y.toString();
y = y + 'px';
ship.style.left = y;
changeColor();
return shipLeft
}
else if ((e.keyCode === 39) || (e === 39)){
y = shipLeft;
shipLeft += 11;
y += 11;
y.toString();
y = y + 'px';
ship.style.left = y;
changeColor();
return shipLeft
}
else if ((e.keyCode === 40) || (e === 40)){
y = shipTop;
y += 11;
shipTop += 11;
y.toString();
y = y + 'px';
ship.style.top = y;
changeColor();
return shipTop;
}
else if ((e.keyCode === 38) || ( e === 38)){
y = shipTop;
y -= 11;
shipTop -= 11;
y.toString();
y = y + 'px';
ship.style.top = y;
changeColor();
return shipTop;
}
}