Chapter 3 · Data

Working with text

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

Step 1 of 22 · Concept

Every character has a number

By the end of this lesson you cut, compare and line up text.

  • Each character has a number, its index, counted from 0: word[0] is "H".
  • word.at(-1) counts from the end: "o", the last character.
  • word[10] is past the end. It gives undefined, not an error.
const word = "Hello";
console.log(word[1], word.at(-1), word[10]);
const word = "JavaScript";
console.log(word.slice(0, 4));
console.log(word.slice(4));
console.log(word.slice(-6));
console.log(word.slice(1, 99));
const word = "JavaScript";
// log the first three letters, the last letter,
// then the word without its first letter
const word = "  banana  ".trim();
word.toUpperCase();
console.log(word);
console.log(word.toUpperCase());
console.log(word.indexOf("an"), word.indexOf("x"));
console.log(word.includes("nan"), word.startsWith("ba"));
let count = 0;
for (const ch of "Ada Lovelace") {
  if (ch >= "A" && ch <= "Z") {
    count++;
  }
}
console.log("Capitals:", count);
const text = "Ada and Alan";
let count = 0;
// add 1 for each "a" or "A" in text
console.log("a:", count);
import { readFileSync } from "node:fs";
const text = readFileSync(0, "utf8");
const lines = text.split("\n");
console.log(lines.length, text.trim().split("\n").length);
function row(name, price) {
  return name.padEnd(8, ".") + price.toFixed(2).padStart(6);
}
const tea = 2.5;
const cake = 12;
console.log(row("Tea", tea));
console.log(row("Cake", cake));
console.log("-".repeat(14));
console.log(row("Total", tea + cake));
import { readFileSync } from "node:fs";
const text = readFileSync(0, "utf8");
// 1. the name, each word with a capital
// 2. a line of = as long as the name
// 3. the initials: A.L.
import { readFileSync } from "node:fs";
const text = readFileSync(0, "utf8");
const words = text.trim().split("\n");
// log the word a dictionary puts first
Terminal
Step 1 of 10Characters by index

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)