Lesson 1 Lab — Translating C# to JavaScript¶
Practice the JavaScript from the guide by translating small C# snippets into JS in your
scratch project. No React, no API — just src/main.ts and the browser Console.
Refer back to the guide for syntax.
Prerequisite: the
js-ts-playgroundscratch project from the guide, running withnpm run devand its DevTools Console open.
Steps¶
-
In
src/main.ts, clear any starter code. You'll log everything withconsole.log(...). -
Variables & equality. Declare
const restaurant = "TableServe";andlet coversTonight = 40;. ReassigncoversTonightto55. Log both. Then log1 === 1and1 === "1"— note the second isfalse(no coercion). -
Template literal. Given
const item = "Ribeye"; const price = 24.99;, build and log`${item} costs $${price}`. -
Object + destructuring. Create
const staff = { id: 1, firstName: "Sam", lastName: "Diaz", isManager: true };. DestructurefirstNameandlastNameout of it and log a full name with a template literal. -
Spread. Make
const promoted = { ...staff, isManager: true, isAdmin: true };and log it. Confirmstaffitself is unchanged (log it too) — spread made a new object. -
The loop evolution. Start from:
Translate this C# in three stages, logging each result: - Stage 1: a
forloop that pushes into a newmarkedarray. - Stage 2:
prices.forEach(...)pushing into a new array. -
Stage 3:
prices.map((p) => p * 1.1)— one line. -
.filter()= LINQ.Where(). From the sameprices, log only those under$16withprices.filter(...). (C#:prices.Where(p => p < 16).) -
Modules. Create
src/money.tsthatexports a namedformathelper — the samefunction format(n: number)from the guide (§9), returning a currency string.import { format }intomain.tsand logformat(24.99)(→$24.99).
Verify in the browser¶
With npm run dev running (verification details are in the guide, section 10):
- Every step prints to DevTools → Console (F12) with no red errors.
- After the spread step, the original
staffobject logs unchanged — proof spread copies rather than mutates. - All three loop stages produce the same array — proof
.map()is just the concise form of theforloop. format(24.99)logs$24.99, imported from another file — proof modules connect files.
Same language, different shape from C# — and exactly the JavaScript you'll be writing in every React component starting in Lesson 3.
Stretch challenges¶
Optional — for when you finish early. Not needed for the capstone. [Reinforce] builds on what you just did; [Reach] goes past the guide and needs some research.
- Chain
.filter()then.map()— [Reinforce] — from an array of menu-item objects, filter to items under$10and then map them to just their names, in one chained expression:items.filter(...).map(...). This is the LINQ.Where(...).Select(...)chain you'll use to shape data before rendering. - Nested destructuring — [Reinforce] — given
const order = { id: 5, staff: { firstName: "Sam" } }, pullfirstNameout in one destructuring statement (const { staff: { firstName } } = order;) and log it. - Sum with
.reduce()— [Reach] — add up an array of prices into a single total with.reduce()(not covered in the guide — you'll need to research it). This is how the running order total is computed later. Reference: Array.prototype.reduce() (MDN). - Optional chaining
?.— [Reach] — given an object whose nested property might be missing, safely read it with?.so you getundefinedinstead of a crash (not in the guide — research it). You'll use this constantly on API data. Reference: Optional chaining (?.) (MDN).
Finished these and want more? See stretch-react-challenges.md for bigger challenges that span the whole React pass.