It's hamster time!
Use JavaScript Switch Statements and Keyboard Events to detect when a user has pressed the up, down, left, and right arrows.
To complete this project, students should have the following:
- Basic understanding of HTML structure and attributes.
- Basic understanding of CSS properties.
- Basic understanding of JavaScript and DOM.
To complete this warmup, fulfill the following requirements:
- Set up your project file structure through the command line.
- Create the following:
- HTML, CSS, JS file
- Folder to store images
- Link all of your files correctly.
- Create a
div
with anid
ofcontainer
. - Inside of this
div
, create the following:
Img
element with anid
ofhamster
.Div
element with anid
oftext
. Input the default text here.
To complete the second part of this warmup, fulfill the following requirements:
- In your JS file, create variables to store the image and div element.
- Using the
onkeydown
event on thedocument
, create a function that will take inevent
as a parameter and use a switch statement.
- The switch statement should take in
event.keyCode
as the parameter for the switch.- If the
keyCode
equals to the code of the up arrow, change the source of the image to theup.png
image. Continue to do this for the left, right, and down arrows and their respective images. - Change the
innerHTML
of the text div element to display new text when a different arrow key is pressed!
- If the
https://www.w3schools.com/jsref/event_key_keycode.asp https://www.w3schools.com/js/js_switch.asp