Terminology (5)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.
- Value
- A piece of data a program computes with, such as 10, "10" or true.
- Type
- The kind of a value. It determines which operations exist and what each operator means.
- Concatenation
- Joining text values end to end. "10" and "5" concatenate to "105".
- Integer division
- Division of two whole-number values that discards the fraction, truncating toward zero in Go, Rust and C++.
- number
- TypeScript’s single floating-point numeric type; / on numbers keeps fractions.
Values, types and operations
A value is data a program can use, such as a number, a text string or a Boolean. Its type determines which operations are available and how they interpret it. The characters displayed for a value do not establish its type: text containing digits and a numeric quantity can look alike while supporting different work.
Choose the representation from the field’s job. An identifier may need to preserve its characters; a measurement may need fractional arithmetic. Parsing interprets text as another kind of value, while formatting chooses text for display. Follow the types of the operands at the operation itself, including any conversions that happen before or after it.
01What + means
Can the row label use the same operation as the point total?
Quiz-night staff export team codes, points and player counts as text. You are extending their results tool to report points per player and pick a leader. The printed digits look usable already. Which values need to become numbers, and which must keep their original characters?
Start with the row display. The tool has a numeric points value of 66 and wants to append its decimal digits to the label "points ". The same numeric value can also be added to another points value.
This reduction tries the text operation first and a numeric addition second. Predict the first printed line. Treat a compiler refusal as a result of its own; C++ character-code predictions assume an ASCII-compatible target.
const label = "points ";
const points = 66;
console.log(label + points);
console.log(points + points);
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. The Go and Rust experiments in section 01 are expected to fail to compile. C++ uses the track’s C++26 mode; nothing here needs a newer feature than C++17. Its character-code examples assume an ASCII-compatible execution character set.
02What / keeps
A readable share can still have lost its fraction
The comparison rule is points per player, so a team with eleven points and four players must retain a share of 2.75. Predict the printed lines: does choosing a floating-point destination preserve that fraction, or has the division already discarded it?
These TypeScript operands have the floating-point type number. There is no separate integer type for 11 and 4.
const points = 11;
const players = 4;
console.log(points / players);
console.log(Math.trunc(points / players));
03Text that looks numeric
The share is numeric. Is the leader comparison numeric too?
The importer still holds its original text fields. Before choosing a leader, reduce the comparison to two score strings, "9" and "10". Which one is smaller as text? The remaining lines compare or format deliberate numeric conversions. Predict all three outputs before sorting the results.
const first = "9";
const second = "10";
console.log(first < second);
console.log(Number(first) < Number(second));
console.log(Number(first) + Number(second));
04Repair the results
Shares, team labels, then the leader
Finish the results report with fresh team inputs. First give staff fractional points per player and a count of complete tables of four. Next add row labels in input order while keeping each imported team code; the tenth row must work as well as the first. Finally choose a leader using the bonus-adjusted share, preserving half points and the earlier team on an exact tie.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete caller. Later levels supply the earlier correct helpers.
Repair the quiz-night results
Fractional and whole results, number-to-text labels, then a leader chosen from text, whole-number and fractional values.
In the frontend An input’s value is text until you convert it Optional
A score field uses <input type="number">. The browser still stores its value as text. React’s change event gives that string; Svelte’s bind:value on a number input converts it for you.
"use client";
import { useState } from "react";
export default function Score() {
const [points, setPoints] = useState(0);
return (
<label>
Points
<input
type="number"
value={points}
onChange={(event) => setPoints(event.target.valueAsNumber)}
/>
<output>{points + 1}</output>
</label>
);
}
event.target.value is always a string, so storing it would make points + 1 concatenate. valueAsNumber reads the number; an empty field gives NaN, which a real form must handle.
Sources: MDN: HTMLInputElement valueAsNumber,Svelte: bind:value.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?