TypeScript, lesson 5
A loop runs the same block of code many times. TypeScript has the same loops as JavaScript; the type checker simply keeps an eye on the variables inside them.
forThe three parts in the parentheses are: a start (let i = 1, run once), a condition checked
before each round (i <= 5), and an update run after each round (i++). i is inferred as a
number. Count down with i--, or in steps with i += 2.
while2 exercises to try what you just read.
Use while when you know when to stop but not how many rounds it will take:
Something in the body must eventually make the condition false. An endless loop is stopped by
the runner's time limit, and you get no output at all. There is also do { ... } while (cond);,
which always runs the body at least once.
for...offor...of gives you each element of an array (or each character of a string) in turn:
Because temps is a number[], TypeScript knows t is a number. Use const for the loop
variable: each round gets a fresh one. If you also need the position, loop over
temps.entries(), which gives [index, value] pairs:
Do not confuse for...of with for...in, which loops over property names (and for an array
gives you the indexes as strings). For arrays you nearly always want for...of.
break and continuebreak leaves the loop at once.continue skips to the next round.Loops often accumulate a value: a running total, a count, or a string built piece by piece.
A loop inside another loop (a nested loop) handles grids and tables: the outer loop picks the row, the inner loop walks across it.
Read a whole number N (1 to 12) and print its times table from 1 to 10, one line each,
in the form I x N = RESULT.
Sample input:
Sample output:
Start with a positive whole number. If it is even, halve it; if it is odd, multiply by 3 and
add 1. Repeat until you reach 1. The numbers bounce up and down like hailstones before
landing. (Mathematicians believe every starting number reaches 1, but nobody has proved it.)
Read the starting number and print two lines: the number of steps needed to reach 1, and the
highest value seen along the way (including the start).
Sample input:
Sample output:
The sequence for 6 is 6, 3, 10, 5, 16, 8, 4, 2, 1: eight steps, with a peak of 16. Starting
at 1 needs 0 steps.
for (let i = 1; i <= 5; i++) {
console.log(`${i} x 3 = ${i * 3}`);
}let population = 1000;
let years = 0;
while (population < 2000) {
population *= 1.07; // grows 7% per year
years++;
}
console.log(years); // 11const temps: number[] = [12, 15, 9, 20];
let warmDays = 0;
for (const t of temps) {
if (t >= 15) warmDays++;
}
console.log(warmDays); // 2for (const [i, t] of temps.entries()) {
console.log(`day ${i + 1}: ${t}`);
}for (const word of ["apple", "", "banana", "STOP", "cherry"]) {
if (word === "") continue; // skip blanks
if (word === "STOP") break; // stop completely
console.log(word);
}
// apple
// bananalet row = "";
for (let i = 0; i < 5; i++) {
row += i % 2 === 0 ? "#" : ".";
}
console.log(row); // #.#.#31 x 3 = 3
2 x 3 = 6
3 x 3 = 9
4 x 3 = 12
5 x 3 = 15
6 x 3 = 18
7 x 3 = 21
8 x 3 = 24
9 x 3 = 27
10 x 3 = 306steps 8
peak 16