let or const?
Some values change while a program runs, like how much money you have. Others never change, like the number of days in a week. Declare a value that will change with let, and a fixed value with const (a constant).
const daysInWeek = 7;
let day = 30;
console.log("A week has " + daysInWeek + " days");
console.log("September has " + day + " days");
day = 31;
console.log("October has " + day + " days");A week has 7 days September has 30 days October has 31 days
const num; is an error too.const num = 3; num = 5; console.log(num);
TypeError: Assignment to constant variable.
The not-equal operator !=
A != B means "A is not equal to B". The condition is true when the two values are different:
const weather = "Cloudy";
if (weather != "Rainy") {
console.log("Play soccer");
}Play soccer
The while loop
A for loop suits a known number of repeats. Sometimes we do not know it: "keep buying comics as long as you have enough money". Then we use while (condition) { ... }: it checks the condition, runs the code in the braces if it is true, then checks again.
let money = 2000;
const comics = 425;
while (money > comics) {
money = money - comics;
console.log(money);
}1575 1150 725 300
We can count the repeats with a variable that starts at 0. Here we save 15000 each month until we reach 85000. Note that money += 15000 is short for money = money + 15000:
let money = 30000;
let month = 0;
while (money < 85000) {
month++;
money += 15000;
}
console.log("Save for " + month + " months");Save for 4 months
| Loop | Use it when |
|---|---|
| for | we know how many times to repeat |
| while | we repeat until a condition changes, without knowing the count |
money). If you forget, the condition stays true forever and the loop never ends.Practice
Write let or const in the blank. Careful: day will change to 31.
___ day = 30;
console.log("September has " + day + " days");
day = 31;
console.log("October has " + day + " days");Press Run to see the result. Run the program and you will see an error. Fix the declaration on the first line so it prints 5.
Hint
num changes, so it is not a constant.const num; num = 3; num = 5; console.log(num);
Press Run to see the result. Write the condition: if myScore is not equal to 100, print Aim for a perfect score!
const myScore = 90;
if (___) {
console.log("Aim for a perfect score!");
}Press Run to see the result. Complete the loop: buy a snack and print the money left as long as money is greater than snacks.
let money = 1000;
const snacks = 230;
___ (___) {
money = money - snacks;
console.log(money);
}Press Run to see the result. You save 800 a month. Complete the loop so it keeps saving as long as money is less than 4000; it prints Save for 5 months.
let money = 600;
let month = 0;
___ (___) {
month++;
money += 800;
}
console.log("Save for " + month + " months");Press Run to see the result. A novel has 230 pages and you read 40 pages a day. Use while to find the number of days, then print Finished in 6 days (with the number worked out by the loop).
Hint
Repeat whilepages > 0: subtract 40 and add 1 to days.let pages = 230; let days = 0;
Press Run to see the result. One student knows a piece of school news, and the number who know it doubles every hour. Start n at 1; print n, then double it, as long as n is less than 100. (It prints 1, 2, 4 up to 64, one per line.)
Press Run to see the result. Key points
letfor a value that will change,constfor a fixed one; giving a constant a new value is an error.A != Bis true when A is not equal to B.while (condition) { ... }repeats as long as the condition is true; use it when the count is unknown.- Change something in the condition inside the loop, or it never stops.