Terminology (3)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Logical length
- The current sequence count used to establish its valid element positions; JavaScript Array length can include absent slots.
- Capacity
- Storage room available for elements without requiring growth of that storage; it need not equal the number of existing elements.
- Occurrence
- One element at a particular position in a particular version of a sequence, distinct from another element with an equal value.
Elements, storage, and positions
A sequence keeps elements in order, and an index names a position in that order. A fixed-size array and a growable collection offer different ways to change that sequence; familiar index syntax does not make their construction or growth APIs interchangeable.
Distinguish the logical extent, the elements actually present and any spare storage reserved for growth. Also distinguish an element’s value from its position: inserting or removing an earlier element changes the coordinates used by a saved selection. Equal values can still represent separate occurrences.
01Fixed and growable sequences
Growth changes the sequence, and native APIs return different things
A tile-map editor stores each horizontal row as numeric palette codes. Code 0 is a blank tile. Appending a tile, inserting a blank margin and erasing tiles selected from the displayed row all change the same sequence. Equal codes can occur at different selected positions.
The tile-map row editor adds code 9 after two existing codes. Predict the length and last value from the actual native operation.
const row = [6, 2];
const length = row.push(9);
console.log("length", length, "last", row[row.length - 1]);
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts
Save one complete example at a time in a fresh folder under the selected filename. Run the shown checking/build command before execution. The browser lab supplies its editor, stdin fixtures and native runner.
02Elements and spare room
A length or reservation does not always mean initialized data
The editor next needs a blank margin before the grown row. This reduced construction probe requests room for four positions using the selected native’s API. Predict which elements actually exist before that space is used as tile data.
const row: number[] = new Array(4);
let visited = 0;
row.map((value) => {
visited += 1;
return value + 1;
});
console.log("length", row.length, "visited", visited);
03Positions through structural edits
A saved position can select another occurrence
The editor remembers position 1 and its value, then inserts one code at the front. Predict what the position and copied value mean afterward.
const row = [6, 2, 9];
const selected = 1;
const remembered = row[selected];
row.unshift(5);
console.log("position", row[selected], "remembered", remembered);
04Practice
Repair the tile-map row editor
Repair the tile-map row editor: append a palette code, create an initialized blank margin, then erase the selected occurrences from the row the user saw. A repeated selection names one occurrence; an equal code elsewhere remains a separate tile.
The supplied caller prints the row after each operation. Code 0 denotes a blank cell, and saved positions refer to the displayed row before deletion.
Start the lab
Append in native syntax, create real margin elements, then preserve displayed selection semantics.
In the frontend Array mutation results and framework state publication are separate operations. Optional
Adding a display code is also useful in a UI. React publishes a replacement array; Svelte deep state can observe an array mutation. Neither framework changes what the native Array operation returns.
'use client';
import { useState } from 'react';
export function DisplayRow() {
const [codes, setCodes] = useState([6, 2]);
function add() {
setCodes((previous) => [...previous, 9]);
}
return <button onClick={add}>{codes.join(', ')}</button>;
}
Supply a new array value to the setter. The numeric return from push is not that replacement array.
Sources: React: Updating arrays in state,Svelte: Deep state.
Sign in to keep your read marks on every device. Sign in
Was this helpful?