Two flatmates. Two points of view. One of them runs your JavaScript.
Clive can't jump much. Jazz can jump, and that's about it. Swap between their heads to get the pair of them across Croydon, and write the script that the one you aren't playing runs, or he'll wander back to the sofa.
Works in credit at Croydon Credit. Owns a tie for weekends. Jumps like a man carrying a briefcase, because he usually is. Left alone, he drifts back to his armchair.
Jazz
Would-be rock god, between bands. Jumps high and does very little else. Left alone, he drifts back to the sofa, where his real work happens.
Open his laptop
Laptops around each stage open the other flatmate's to-do list as real JavaScript, with blanks to fill. Pick what goes in each one.
Run it, then swap
Run the script and you swap into the other head. He does what you wrote while you play his flatmate, in JavaScript's order: his lines first, then timers, listeners and awaits.
Listen to the inner voice
Each flatmate thinks out loud along the top of the screen. Some thoughts open a lesson card, with the code and the idea behind it, in whoever's voice you're in.
The Croydon line
Six stops, six pieces of JavaScript
The game is a tram line across Croydon, and each stop teaches one idea. They build on each other, from calling a method to awaiting two promises at once. Every stop has a laptop below, with one of its scripts to try.
The kitchen door only stays open while someone stands on the doormat, and left alone Jazz drifts back to the sofa. So Clive scripts him onto the mat. In the hall the floorboards are up, a hole five boards wide that Clive can't jump. Jazz has to walk to the edge, jump it and step onto the rug that folds the boards down, in that order.
jazz.js Jazz runs this.
// To the edge, over the hole, onto the rug.
jazz.walk(3);
jazz.walk(1);
Options:
Show the answer
jazz.jump();. Jazz walks to the edge, clears the hole and lands on the rug. The boards fold down for Clive.
Clive's inner voice
Objects and methods
jazz.walk(7);
jazz.jump();
jazz is an object, and walk and jump are its methods: things it can do. The dot means 'jazz, do this'. The number in the brackets is an argument: how far. It's like training a dog, if the dog owned a guitar and owed you rent.
Jazz's inner voice
One thing at a time
jazz.walk(3);
jazz.jump();
jazz.walk(1);
Lines run top to bottom, and I do them in that order: walk, then jump, then walk. Each line puts one thing on my to-do list. Swap two lines and you get a different afternoon. Usually a worse one.
2 Croydon Credit
Croydon Credit Variables & strings
Jazz has come into Clive's office to ask for a loan, which Clive is not allowed to give him. The staff door's keypad wants the desk number, then the extension, and the building's laptop builds the code from two constants. Past the door, the lift goes to whatever floor its script says, but only for a number: "2" in quotes is a string, and the lift refuses it.
`${desk}${ext}`. The keypad gets "127": the value of desk, then the value of ext. The staff door opens.
Jazz's inner voice
Labelled boxes
const desk = 12;
let mood = "hopeful";
mood = "less hopeful";
console.log(desk, mood); // 12 less hopeful
const and let give a value a name. A const keeps the same value forever, like Clive's haircut. A let can be given a new one with =, like my plans for the evening.
Backticks make a template literal: text with holes in it. Inside ${ }, a name is swapped for its value. Outside, desk is just four letters, the way Gary is just four letters, not a personality.
2 is a number. "2" in quotes is a string: text that happens to look like a number. Add 1 to a string and JavaScript glues it on the end. The lift only takes numbers. The lift and I have standards.
3 The Cellar Bar
Band Practice Loops
The band's stage in the cellar bar is five tiles up. Every strum lifts the speaker stack one notch, so a loop that strums four times carries Clive up to where he can climb off. Five is too loud, and the landlord pulls the plug. Up top, three amps each raise a riser into a stair, and only a for...of over the whole array switches on all three.
jazz.js Jazz runs this.
// Each strum lifts the speakers one notch.for (let i = 0; i 4; i++) {
jazz.strum();
}
Options:
Show the answer
<. i goes 0, 1, 2, 3: four strums, four notches, and Clive climbs off onto the stage.
Jazz's inner voice
The for loop
for (let i = 0; i < 3; i++) {
console.log("strum", i);
}
// strum 0, strum 1, strum 2
let i = 0 starts the count. i < 3 is checked before every lap, and i++ adds one after it. So i goes 0, 1, 2: three laps. Write i <= 3 and you get four. Every drummer I've ever played with has made that mistake.
Clive's inner voice
for...of
const amps = ["amp1", "amp2", "amp3"];
for (const amp of amps.slice(1)) {
console.log(amp);
}
// amp2, amp3
for...of goes through every item in an array, one per lap. slice(1) makes a new array starting at index 1, so it skips the first. And amps[0] isn't an array at all, it's one amp. You can't loop over one amp. Jazz has tried.
4 Wet Bottom Farm
The Stag Do Events & callbacks
Two farm gates, one behind the other, and two plates up in the hay loft, one for each gate. Clive needs Jazz on the first plate until he's between the gates, then on the second. A plain list of steps can't wait for that. A listener can: when Clive rings the bell in the pen, the callback walks Jazz along the loft.
An event is something that happens: a bell rings, a door opens, Gary cries. addEventListener says: when this happens, run this function. The name has to match exactly. Listen for "ding" and you'll wait forever. I've waited forever before. It was a Tuesday.
() => { } is a function with no name: an arrow function. Handing one to addEventListener doesn't run it. The bell runs it later, every time it rings. That's a callback: don't call us, we'll call you. The story of my career.
5 East Croydon
East Croydon Timers & the event loop
The ticket barrier opens for one second when someone presses the help button, and the button is a long walk from the barrier. setTimeout presses it later, once Clive has had time to get there. On the platform, the train doors open when the tannoy hears "mind", "the", "gap!" in that order, and the laptop logs "gap!" from a setTimeout of 0.
jazz.js Jazz runs this.
// Jazz is at the help button.setTimeout(() => {
jazz.press();
}, );
Options:
Show the answer
2500. Two and a half seconds later Jazz presses the button, just as Clive reaches the barrier. It opens for him.
setTimeout runs a function later. The number is milliseconds, thousandths of a second, so 2500 is two and a half seconds. Meanwhile the rest of the script carries straight on. It doesn't wait. Nobody waits for me either.
Jazz's inner voice
The event loop
setTimeout(() => console.log("C"), 0);
console.log("A");
console.log("B");
// A, B, C
A timer of 0 doesn't mean now. It means as soon as you're done with everything else. JavaScript finishes the lines it's on, then looks at its list of things to do later. Like me with the washing up.
6 St Agatha's
The Wedding async & await
Tess from Accounts is marrying Gary from Sales. Jazz is the music, and Clive, against his better judgement, is doing a reading. Jazz has to play the march once Clive reaches the altar, and await is how his script waits. Out the back, the getaway car leaves on car.drive(), so it should wait on Promise.all of both flatmates reaching it, or it goes with nobody in it.
A promise is a value that turns up later, like a wedding present or an apology. Inside an async function, await pauses until it turns up. Without await, the next line runs straight away. Jazz runs straight away. Always has.
Promise.all takes a list of promises and waits for every one. Clive reaching the car is one promise. Me reaching the car is another. The car only goes when both are kept. Like a marriage, if you think about it. I try not to.
14 lesson cards · 6 stops · every snippet checked against Node
Controls
How to play
Action
Keyboard
Gamepad
Move
A / D or ← / →
D-pad or left stick
Jump
Space, W or ↑
A / Cross
Swap heads
Tab or Q
Y / Triangle or RB
Open a laptop, ring a bell, pull a lever
E
X / Square
Run the script
R
LB / L1
Pause
Esc or P
Start / Options
On a phone or tablet, turn it sideways: the on-screen buttons include Swap and Code. A fall sends you back to the last Wi-Fi router you passed. Each stop hides six custard creams. Your progress saves in this browser, separately from Dunder Quest's.
Flatmates.js is the second Dunder Quest game. The first, Dunder Quest, teaches Python on a quest for the Grail: slice a list to lay a bridge, loop a lift up a cave wall, and duel the Bridgekeeper. The third, Curb Your Oxidation, teaches Rust on the Westside of LA, and the fifth, Prince of Versions, teaches Git with a dagger that turns back time.
Flatmates.js is an homage to a certain Croydon flat-share sitcom. The names and jokes are winks, not the show's own characters or lines, and it isn't affiliated with or endorsed by anyone who made it.