Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active April 16, 2024 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/92af426eb489130835b3ea2701865635 to your computer and use it in GitHub Desktop.
Save MisterBrash/92af426eb489130835b3ea2701865635 to your computer and use it in GitHub Desktop.
ICS3U Lesson 3.3 - Do While Loops (and extras)

3.3 - Do While

ICS3U - Mr. Brash 🐿️

Before we begin, let's cover the isNaN() function. It has been mentioned in passing but here's how it works:

  • isNaN(value) will return true if the value is not a number.
  • It will return false if the value 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.");
}

Lesson

Jump to the task

Recall the While Loop:

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);

❓ What might be wrong with that example above?

More Examples

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 and output declared outside of the loop?
  • Can you think of a potential problem or small bug with the build_string() function?

Your Tasks

Jump back to the lesson

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.

negativeOnly()

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.

printOdd(n)

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

parrotUntilQuit()

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.

factorial(n)

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.



🐿️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment