Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active April 17, 2024 02:26
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/2bb7600484c0494671f4ea8fcbb28990 to your computer and use it in GitHub Desktop.
Save MisterBrash/2bb7600484c0494671f4ea8fcbb28990 to your computer and use it in GitHub Desktop.
ICS3U Lesson 3.1 - While Loops

3.1 - While Loops

ICS3U - Mr. Brash 🐿️

The Lesson

Jump to the task

Repeating your code is inefficient. Also we might not know when it will end:

// Ask the user to enter a number and check for valid input
let input = Number(prompt(“Enter a number.));

if (isNaN(input))
  input = Number(prompt(“Please enter a number.));
    if (isNaN(input))
      input = Number(prompt(“Ugh, please enter a number.));
        if (isNaN(input))
          input = Number(prompt(“Enter a NUMBER.));
            if (isNaN(input))
              ... 

For this reason, programming languages have the ability to move back up to a specific line in the code. Theoretical Loop

It's not as simple as saying "go back to line 12", but it's still pretty simple:

While Look Syntax

Think of the condition just like an if-statement! The code will loop (repeat) and continue to loop if (or while) the condition is true. Let's look at some examples:

// Example: A never-ending loop (not good)
while (true) {
  console.log("You can't stop me!");
}

// Example: Counting to 10
let n = 0;
while (n <= 10) {
  console.log("n is", n);
  n++;  // We need to make sure we modify 'n'!
}

// Example: Should I stop?
let input = "n";
while (input != "y") {
  input = prompt("Should I stop? (y/n)").toLowerCase();
}
console.log("Ok, I stopped");

Your Turn!

Back to the lesson

Create a new code environment / file in VSCode. (FILE > New Window or FILE > Open Folder). I recommend making a new folder called Unit 3 and this first code file could be 3.1-While.js or something similar.

The top of your file should have a header. Something like this:

/**
 * 3.1 - While Loops
 * Author: John Smith
 * 
 * Lesson link:  go.brash.ca/3U-3.1-While-Loops
 **/

You should also get used to using the 'use strict'; directive at the top of your code (you've seen this in previous assignments).

Your Task:

Note - if you struggle with the functions below, see your teacher for simpler examples / tasks.

Create the following three functions:

  1. countdown(start, stop): this function will count down from the start value to the stop value.
  • Check start and stop to make sure start > stop.

    • If not, return -1.
  • When the loop finishes, the function should return the number of times it looped (how many numbers it printed).

    For Example:

    countdown(9, 4)
    9
    8
    7
    6
    5
    4
    6   << That's the return value (only visible if you use console.log())

  1. random_until(min, max, stop): this function will...

    • check that max is greater than min. If not, return -1;
    • check that stop is between (or equal to) min and max. If not, return -1;
    • generate a random number from min to max. Print it to the console.
      • Repeat this process as long as the random number is not equal to stop;
    • return stop.

    You're going to need the randInt() function. Copy and paste it from here:

    // Return a random integer from min to max (inclusive)
    function randInt(min, max) {
      min = Math.ceil(min);
      max = Math.floor(max);
      return Math.floor(Math.random() * (max - min + 1) + min);
    }

  1. average(n) - this function asks the user for n values and then calculates the average, not rounded. It should behave like this:
    > average(5)
    Please enter value 1/5 > 6
    Please enter value 2/5 > 4
    Please enter value 3/5 > 2
    Please enter value 4/5 > 4
    Please enter value 5/5 > 12
    The average is 5.6
    It is safe to assume proper input - no need to check that it's a valid number.

Sir, this is too easy for me - I'm kinda bored in your class.

You have two things to learn that will greatly increase your abilities as a programmer:

  • 2-dimmensional arrays. Python calls them lists but most languages call them arrays. If you have not learned about arrays yet, start with 1-dimensional (normal) arrays. Then learn how 2D arrays work.
  • HTML. Learn how to make websites with multiple pages and start diving into things like the <div> tag, CSS, and then get into EventListener. There are sooooo many tutorial sites or videos out there.

I don't want to learn HTML: Then make sure you know 2D-Arrays. Once you do, talk to Mr. Brash about the contest questions.


🐿️

// Just in case the code is needed outside of the README.md
// Return a random integer from min to max (inclusive)
function randInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment