Back to Lesson # 9 - Arrays | Table of Contents | # 9 Review Questions |
For these exercises, you will need a blank web page to start.
1. Use the sample from the lesson of the array containing a deck of Cards. Write a JavaScript function to 'deal' a hand of 5 cards at random. How you say? Well, break in down into steps - top-down design philosophy in action.
2. Let's do a game of skill instead of chance - using your power over the world of arrays. It's called "Name my Capital City". Many things are pretty easy using two arrays set up to match. For example, we could set up one array to hold all names of the states in the US, and another array to hold the names of the state's capitals. The key ingredient is that the state and its capital are in the same location.
Write a web page with a form containing two input text boxes and two buttons, one called "Start Game" and one called "Check My Answer". When the user clicks on "Start Game" they get 10 rounds. In the first text box, a random state appears and the program waits for the user to type in the state capital city's name. The catch is that it must be spelled correctly, or JavaScript will count it wrong. There should be a var called score that can be displayed in an alert message box at the end to tell the user their score out of 10 when the game is done.
For a complete list of states and capitals, visit https://state.1keydata.com/state-capitals.php. There are also some fun things done with image maps so that you can see what some other web designers have done with this idea. Check out https://www.w3schools.com/tags/tag_map.asp to see an image map.