WDI Fundamentals

WDI Fundamentals Unit 8


Your Turn Your Turn

Switch Statements and Ternary Operators Exercise


Instructions

  1. Write the code to perform the actions listed below in the "JavaScript" panel in the JS Bin editor.
  2. If you don't see the JS Bin below, please refresh the page.
  3. Click "Run" to run the code in the "Console" panel.
  4. After clicking "Run", to check variable values, type the variable name in the "Console" panel on the right and hit enter/return.

JS Bin on jsbin.com

Let's get started!

Part 1 - Switch Statements
  1. Declare a variable favoriteMovie and assign it the value "star wars".
  2. Declare a variable moviePhrase. It should have no initial value.

    Hint: The syntax to declare a variable with no initial value looks like this: var myVariableName;.

  3. Write out a switch statement for the conditions that are written in pseudo code:

     // If favoriteMovie is "jaws" 
           // Assign moviePhrase the value "You're gonna need a bigger boat." 
     // Else if favoriteMovie is "the shining" 
           // Assign moviePhrase the value "All work and no play makes Jack a dull boy."
     // Else if favoriteMovie is "star wars"
           // Assign moviePhrase the value "Do. Or do not. There is no try." 
     // Else if favoriteMovie is "forrest gump" 
           // Assign moviePhrase the value "Life was like a box of chocolates."
     // Else if favoriteMovie is "back to the future" 
           // Assign moviePhrase the value "Where we're going, we don't need roads."
     // Else
           // Assign moviePhrase the value "Great movie choice!"
    
  4. Hit the "Run" button. Then type moviePhrase; into the "Console" panel and hit enter/return to check to make sure you are getting the expected result. Try assigning different values to the favoriteMovie variable to make sure everything is working correctly.
Part 2 - Ternary Operators
  1. Declare a variable hasEmptySquares and assign it the value false.
  2. Declare a variable answersAreCorrect and assign it the value true.
  3. Declare a variable message. It should have no initial value.
  4. Write a ternary statement for the conditions that are written in pseudo code:

     // If hasEmptySquares is equal to false and answersAreCorrect is equal to true 
           // message ="Crossword puzzle complete!"; 
     // Otherwise 
           // message = "Not quite!";
    
  5. Hit the "Run" button. Then type message; into the "Console" panel and hit enter/return to check to make sure you are getting the expected result. Test out different values for hasEmptySquares and answersAreCorrect to make sure everything is working correctly.

Stuck? Check out the solutions for assistance.


On to the next lesson!