Programming ProblemsWhile LoopsDo While LoopsFor LoopsNested LoopsSolutionsWhile loopsDo While LoopsFor LoopsNested Loops
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.
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.
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;
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.
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.
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.
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".
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.
Write a for loop that prints the even numbers from 2 to 20 to the console.
Write a for loop that calculates the product of the numbers from 1 to 5, and then prints the product to the console.
Write a for loop that prompts the user to enter a string, and then prints the string with each word capitalized.
Write a for loop that prompts the user to enter a string, and then counts the number of vowels in the string.
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.
Write a nested while loop that prompts the user to enter a string, and then checks if the string is a palindrome.
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.
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.
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.
let number = prompt("Enter a number greater than 10");
while (number <= 10) {
number = prompt("Please enter a number greater than 10");
}
console.log(number);
let input = prompt("Enter a string ending in a period");
let i = input.length - 1;
let backwards = "";
while (i >= 0) {
backwards += input.charAt(i);
i--;
}
console.log(backwards);
let sentence = prompt("Enter a sentence:");
let i = 0;
let capitalized = "";
while (i < sentence.length) {
let char = sentence.charAt(i);
if (i === 0 || sentence.charAt(i-1) === " ") {
capitalized += char.toUpperCase();
} else {
capitalized += char;
}
i++;
}
console.log(capitalized);
let password;
do {
password = prompt("Enter a password with at least 8 characters:");
} while (password.length < 8);
let number;
do {
number = parseInt(prompt("Enter a positive number:"));
} while (number <= 0);
let input;
do {
input = prompt("Enter a string, or type 'quit' to exit:");
if (input !== "quit") {
console.log(input.toUpperCase());
}
} while (input !== "quit");
let input;
let sum = 0;
do {
input = parseInt(prompt("Enter a number (or 0 to exit):"));
sum += input;
} while (input !== 0);
console.log(sum);
for (let i = 2; i <= 20; i += 2) {
console.log(i);
}
xxxxxxxxxx
let product = 1;
for (let i = 1; i <= 5; i++) {
product *= i;
}
console.log(product);
xxxxxxxxxx
let input = prompt("Enter a string:");
let words = input.split(" ");
let output = "";
for (let i = 0; i < words.length; i++) {
let word = words[i];
output += word.charAt(0).toUpperCase() + word.slice(1) + " ";
}
console.log(output.trim());
xxxxxxxxxx
let input = prompt("Enter a string:");
let vowelCount = 0;
for (let i = 0; i < input.length; i++) {
let letter = input.charAt(i).toLowerCase();
if (letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u") {
vowelCount++;
}
}
console.log("There are " + vowelCount + " vowels in the string.");
xxxxxxxxxx
for (let i = 1; i <= 5; i++) {
let row = "";
for (let j = 1; j <= i; j++) {
row += "*";
}
console.log(row);
}
xxxxxxxxxx
let input = prompt("Enter a string:");
let isPalindrome = true;
for (let i = 0; i < input.length / 2; i++) {
if (input.charAt(i) !== input.charAt(input.length - i - 1)) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
console.log(input + " is a palindrome.");
} else {
console.log(input + " is not a palindrome.");
}
xxxxxxxxxx
let number = parseInt(prompt("Enter a positive integer:"));
let factor = 1;
while (factor <= number) {
if (number % factor === 0) {
console.log(factor);
}
factor++;
}
xxxxxxxxxx
let num;
do {
num = parseInt(prompt("Enter a number between 1 and 10: "));
} while (num < 1 || num > 10);
let i = 1;
do {
let j = 1;
do {
console.log("*");
j++;
} while (j <= i);
i++;
} while (i <= num);
Beginning JavaScript Course Materials by Brian Bird, written winter 2023, are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.