Chapter 2 · Decide, repeat, reuse

Decisions and loops

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 22 · Concept

Questions with true or false answers

By the end of this lesson your programs decide and repeat. Both start with a condition: a question answered true or false.

conditionmeanswith a = 7, b = 10
a === bequalfalse
a !== bnot equaltrue
a < bless thantrue
a > bmore thanfalse
a <= bat mosttrue
a >= bat leastfalse
const temp = 18;
if (temp > 25); {
  console.log("Hot");
}
console.log("Done");
const score = 72;
if (score >= 90) {
  console.log("Grade: A");
} else if (score >= 70) {
  console.log("Grade: B");
} else {
  consle.log("Grade: C");
}
const x = 7;
console.log(x >= 7 && x < 10, !(x > 10));
console.log(5 == "5", 5 === "5");
const answer = "no";
if (answer === "y" || "yes") {
  console.log("Yes");
} else {
  console.log("No");
}
let score = 105;
if (score >= 90) {
  console.log(score, "Grade: A");
} else if (score >= 70) {
  console.log(score, "Grade: B");
} else {
  console.log(score, "Grade: C");
}
score = -5;
if (score >= 90) {
  console.log(score, "Grade: A");
} else if (score >= 70) {
  console.log(score, "Grade: B");
} else {
  console.log(score, "Grade: C");
}
for (let i = 0; i < 3; i++) {
  if (i % 2 === 0) {
    console.log(i, "even");
  } else {
    console.log(i, "odd");
  }
}
let total = 0;
// add every number from 1 to 100 to total
console.log("Sum:", total);
let n = 10;
while (n < 100) {
  console.log(n);
  // something is missing here
}
console.log("Past 100:", n);
let n = 6;
// while n is more than 1:
//   log n
//   halve n if it is even, else make it 3 * n + 1
console.log(n);
for (let n = 1; n <= 15; n++) {
  console.log(n);
}
// 1. loop i from 1 to 7: n is 7 * i
// 2. log n, "*" when n is even or over 40
// 3. log Marked: and how many got a *
console.log("Go!");
Terminal
Step 1 of 11Try it: the stray semicolon

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)