flatmates.js Play
Pixel art of two flatmates, one in a suit with his arms folded and one holding a guitar, by their front door on a tower block landing at dusk

flatmates.js

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.

Free, no signup. Keyboard, gamepad or touch.

The trailer

Earlier that Saturday

Flatmates.js trailer thumbnail: Clive and Jazz on their sofas, under 'Learn JavaScript. Blame your flatmate.' and jazz.walk(7);Play the Flatmates.js trailer

More on the channel: youtube.com/@dunderquest.

Two heads, one flat

You're whichever one you aren't scripting

  1. A laptop's editor open on jazz.js: a for loop with a blank where the comparison goes

    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.

  2. The stag do at a farm: Clive in the pen below, Jazz up in the hay loft by two plates

    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.

  3. Clive's inner voice card: Objects and methods, with jazz.walk(7) and jazz.jump()

    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.

  1. Hermes HouseCalling methods
  2. Croydon CreditVariables & strings
  3. The Cellar BarLoops
  4. Wet Bottom FarmEvents & callbacks
  5. East CroydonTimers & the event loop
  6. St Agatha'sasync & await

1 Hermes House

Flat 5B Calling methods

Flat 5B, as the stage opens

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

Croydon Credit, as the stage opens

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.

door.js The building runs this.
const desk = 12;
const ext = 7;
const code = ;
keypad.enter(code);
Options:
Show the answer

`${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.

Clive's inner voice

Template literals

const desk = 12;
const ext = 7;
console.log(`${desk}${ext}`); // 127
console.log(`desk${ext}`);    // desk7

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.

Clive's inner voice

Numbers and strings

console.log(2 + 1);      // 3
console.log("2" + 1);    // 21
console.log(typeof "2"); // string

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

Band Practice, as the stage opens

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

The Stag Do, as the stage opens

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.

jazz.js Jazz runs this.
jazz.walk(2);
bell.addEventListener(, () => {
  jazz.walk(4);
});
Options:
Show the answer

"ring". Clive rings the bell, the callback runs, and Jazz walks along the loft to the second plate.

Clive's inner voice

Events

bell.addEventListener("ring", () => {
  console.log("ding dong");
});
bell.ring(); // ding dong

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.

Clive's inner voice

Callbacks

const cheer = () => console.log("hooray");
console.log("before");
cheer();
cheer();

() => { } 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

East Croydon, as the stage opens

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.

Clive's inner voice

setTimeout

setTimeout(() => {
  console.log("now");
}, 2500);
console.log("first");

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

The Wedding, as the stage opens

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.

jazz.js Jazz runs this.
async function ceremony() {
   clive.reach(altar);
  jazz.strum();
}
ceremony();
Options:
Show the answer

await. The function pauses until Clive reaches the altar, then Jazz plays the march, on cue for once.

Clive's inner voice

async and await

const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
async function ceremony() {
  await sleep(100);
  console.log("march");
}
ceremony();
console.log("waiting"); // waiting, march

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.

Jazz's inner voice

Promise.all

const sleep = (ms) => new Promise((done) => setTimeout(done, ms));
async function getaway() {
  await Promise.all([sleep(100), sleep(200)]);
  console.log("drive");
}
getaway();

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

ActionKeyboardGamepad
MoveA / D or ← / →D-pad or left stick
JumpSpace, W or ↑A / Cross
Swap headsTab or QY / Triangle or RB
Open a laptop, ring a bell, pull a leverEX / Square
Run the scriptRLB / L1
PauseEsc or PStart / 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.

Play Flatmates.js

a Dunder Quest game

Prefer Python?

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.