Assignment 9

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.

  1. Create and fill the array of 52 cards
  2. Set the end of the array at 51
  3. Generate a random number between 0 and the end of the array.
  4. Display the card at that location in the array
  5. Swap that card with the last card in the array
  6. Decrease the size of the array by 1(this keeps you from gettng the same card twice)
  7. Repeat steps 3 - 6 until you have 5 cards - done!!

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.

 

Return to Top of Page | Back to Main Menu