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.
It's not as simple as saying "go back to line 12", but it's still pretty simple:
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");
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).
Note - if you struggle with the functions below, see your teacher for simpler examples / tasks.
countdown(start, stop)
: this function will count down from thestart
value to thestop
value.
-
Check
start
andstop
to make surestart > 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())
-
random_until(min, max, stop)
: this function will...- check that
max
is greater thanmin
. If not, return -1; - check that
stop
is between (or equal to)min
andmax
. If not, return -1; - generate a random number from
min
tomax
. Print it to the console.- Repeat this process as long as the random number is not equal to
stop
;
- Repeat this process as long as the random number is not equal to
- 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); }
- check that
average(n)
- this function asks the user forn
values and then calculates the average, not rounded. It should behave like this:It is safe to assume proper input - no need to check that it's a valid number.> 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
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 intoEventListener
. 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.
🐿️