Practice Problems Using Selection in JavaScript

These problems are designed to give you practice using the ternary operator, if statements, and switch statements.

Table of Contents

Programming Problems

The solutions to all of these problems are at the bottom of the page. The best way to learn from thesis is to solve these problems without looking at the solutions. When you are done you can compare your solutions to the solutions at the bottom of the page.

You can also use the solutions to these problems as examples to help you understand loop concepts so you can solve other problems.

Ternary Statements

  1. Write a program that checks to see if a number is between two values using the ternary operator.

  2. Write a program that checks to see if a string is empty or not using the ternary operator.

  3. Write a program that checks to see if a string contains a specific character using the ternary operator.

If Statements

  1. Write a program that checks to see if a given number is positive, negative, or zero. If the number is positive, print "The number is positive". If it is negative, print "The number is negative". If it is zero, print "The number is zero".

  2. Write a program that takes two numbers as input and prints the larger number. If the two numbers are equal, print "The numbers are equal".

  3. Write a program that takes a user's age and for the following ages prints a messge letting them know what activities they are old enough to do:

    • Under 18: not old enough to vote

    • 18 and over, but under 21: old enough to vote, but not to drink.

    • 21 and over, but under 25: old enough to drink, but not old enough to rent a car.

    • 25 and over: can vote, drink, and rent a car.

       

Switch Statements

  1. Write a program that checks to see if a character is a vowel or consonant using switch statements.

  2. Write a program that takes a number as an input and outputs the corrsponding name of a day of the week as output.

 

Nested If Statements

  1. Write a program that checks to see if a number is divisible by both 3 and 5 using nested if statements.

  2. Write a program that checks to see if a number is positive and even using nested if statements.

  3. Write a program to suggest that a person wear a wool coat if the temperature is below 45 F. or use an umbrella if it is raining, or wear a rain coat if it is below 45 and raining.

 

Solutions

These are solutions to the problems above. Note that for each problem, there may be more than one good solution, so if you came up with a different solution that works, it isn't wrong. But, you should compare your solution to this one and see which one is simplest and easiest to understand.

Ternary Statements

These solutions use constants as input (rather than prompting a user for input) and use the ternary operator in the solution to the problem. Output is logged to the console.

  1.  

 

If Statements

These programs prompt the user to enter input using the prompt function, which returns a string. We then use the parseInt function to convert the string to a number. Finally, we use if statements to determine the appropriate output and print it to the console using the console.log function.

  1.  

  2.  

Switch Statements

  1.  

  1.  

 

Nested If Statements

  1.  

  2.  

     


Creative Commons License Beginning JavaScript Course Materials by Brian Bird, written sprint , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.