Exercise: JS Selection Statements
Objective#
Your objective is to practice JavaScript selection statements with number guessing validation logic. The user should be prompted to give a number guess, and receive feedback whether the number they guessed was the correct number or not.
Steps#
Clone the exercise repo: JavaScript Selection Statements
Prerequisites#
JavaScript has a built-in function for taking input from a user on a webpage. window.prompt, or simply prompt, will display a prompt to the user on a webpage and give them an input box for submitting data. You can store this data in a variable, or use it directly in your program.
prompt("What is your name?") will prompt the user with that question, and return their answer to that point in your program.
Example:
Exercise 1#
- Initialize an integer to represent a favorite number and assign a value with a number of your choosing or you can assign a random number to this variable.
- Use
window.prompt()to ask the user to input a number, and store the result in a variable - Create an if-statement that if the guessed number is below the initial value, print out
"too low". - Create an else-if statement that if the number is higher than the initial value, print out
"too high". - Create an else statement that prints out
"Congratulations!!!".
Exercise 2#
Your goal is to take a user's birth month, and print out the season they were born in.
- Declare a variable named
birthMonthand set the value as the result ofwindow.prompt("What is your birth month?"). - Create a switch-case statement with cases and a default case.
- Print a response to the console that displays the season that the user was born in depending on their birth month input.
Exercise 3#
Given business logic that is written in the form of if/else statements, rewrite the program with switch cases.
Start with the following code:
After you refactor your program to use switch cases inplace of if/else statements, your output should read: