Before we begin, let's cover the isNaN()
function. It has been mentioned in passing but here's how it works:
isNaN(value)
will returntrue
if thevalue
is not a number.- It will return
false
if thevalue
is a number.
Example:
let user_choice = parseInt(prompt("Please enter a number: "));
if (isNaN(user_choice)) {
console.log("Sorry, that entry was not a valid number.");
}
The While loop might never run...
let raining = false;
while (raining) {
console.log("It's raining, I need an umbrella.");
}
// The loop above will not run. Not even once.
Sometimes we want the loop to run at least once. For example, when getting input from the user.
That's where the do...while loop can help:
let input;
do {
input = parseInt(prompt("Please enter a number from 1 to 5"));
} while ((isNaN(input)) || (input < 1) || (input > 5));
// The loop above will run at least once
- Notice that the condition is at the end of the code block for a do...while. Conversely, the while loop has the condition at the beginning.
It is up to the developer to pick the correct loop. Sometimes a while is better, sometimes a do while is better...
Here's another example:
let raining = false;
do {
console.log("It's raining! I need an umbrella.");
} while (raining);
Sometimes you need to use a 'counter' to control the loop. Remember to always modify the counter each loop to avoid an infinite scenario!
// Print "Hi" the given number of times (at least once)
function printHi(num) {
do {
console.log("Hi");
num--; // OR num = num - 1
} while (num > 0);
}
Other times you might use a random number, a string, or input to control the loop:
// Ask the user for a number and validate their input
function getNumber() {
let input;
do {
input = Number(prompt("Please enter a number:"));
} while (isNaN(input));
return input;
}
We can build complicated mathematics or strings using a loop:
// Build a string from user input
function build_string() {
let output = "";
let input;
do {
input = prompt("Enter a string or 'q' to quit: ");
output += input;
} while (input.toLowerCase() != "q")
return output;
}
- Why are the variables
input
andoutput
declared outside of the loop? - Can you think of a potential problem or small bug with the
build_string()
function?
Now it's your turn to practice. The list of functions below is in no particular order. Pick what you want to work on based on your own abilities. Having difficulty? There are so many ways to get help!
Create a new code file for yourself, called 3.3 - Do While.js
.
Write the function negativeOnly()
that asks the user for a negative number and keeps asking for one as long as they enter something other than a negative value. If a negative number is entered, the function ends.
Given some number, n
, as a parameter to the function, print the odd numbers from 1 to n
. If n
is less than 1 or not a number, nothing should print.
For Example:
printOdd(7);
// Prints:
1
3
5
7
printOdd("s'up?");
// Nothing is printed
Prompt the user to enter some text or the word 'quit'.
- If the input is not quit print it back out to them and then prompt them again.
- If the input is quit, print "Goodbye!" to the screen and the program ends.
In mathematics, the factorial of a number is written like this:
n! = n x (n-1) x (n-2) x (n-3) x ... x 2 x 1
For example, 5! = 5 x 4 x 3 x 2 x 1 = 120
Write the function factorial(n)
that calculates and returns the factorial of the given number.
For Example:
factorial(5);
120
factorial(3);
6
factorial(10);
3628800
Fun fact: you can only take the factorial of positive whole values and 0! == 1
.
🐿️