Practice Problems Using Loops in JavaScript

 

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 these 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.

While Loops

  1. Write a while loop that asks the user to enter a number greater than 10, and keeps asking until the user enters a valid number. Then, print the number to the console.

  2. Write a while loop that prompts the user to enter a string, and then prints the string backwards to the console using the .charAt() method. Hint: you can get the number of characters in a string using the length property; like this: int numberChars = someString.length;

  3. Write a while loop that prompts the user to enter a sentence, and then capitalizes the first letter of every word in the sentence. Print the capitalized sentence to the console.

 

Do While Loops

  1. Write a do while loop that prompts the user to enter a password, and keeps prompting until the user enters a password that is at least 8 characters long. Hint: use the string length property.

  2. Write a do while loop that prompts the user to enter a positive number, and keeps prompting until the user enters a number that is positive.

  3. Write a do while loop that prompts the user to enter a string, and then prints the string to the console in all uppercase letters. Repeat the loop until the user enters "quit".

  4. Write a do while loop that prompts the user to enter a number, and then calculates the sum of all the numbers entered by the user. Repeat the loop until the user enters 0.

For Loops

  1. Write a for loop that prints the even numbers from 2 to 20 to the console.

  2. Write a for loop that calculates the product of the numbers from 1 to 5, and then prints the product to the console.

  3. Write a for loop that prompts the user to enter a string, and then prints the string with each word capitalized.

  4. Write a for loop that prompts the user to enter a string, and then counts the number of vowels in the string.

Nested Loops

  1. Write a nested for loop that prints a pyramid pattern with 5 rows, where each row has a number of asterisks equal to the row number.

  2. Write a nested while loop that prompts the user to enter a string, and then checks if the string is a palindrome.

  3. Write a nested while loop that prompts the user to enter a positive integer, and then prints the factors of the number to the console.

  4. Write a nested do-while loop that prompts the user to enter a number between 1 and 10, and then prints a right triangle of asterisks with the number of rows equal to the input value.

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.

While loops

  1.  

 

Do While Loops

  1.  

For Loops

 

Nested Loops

  1.  


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