Quantcast
Channel: Keyboard handler to move a shape in response to arrow keys - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 3

Keyboard handler to move a shape in response to arrow keys

$
0
0

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;    } }

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>