Search HoldMyTwist

Browse all posts

Episode 2

The Missing Boot

A duck is about to start packing his suitcase for a puddle parade. The green light on the suitcase indicates that he is ready to go, but one boot is missing.

Episodes

Computing Basics

    Duck hears the parade and calls from the workshop monitor. Duck says, “Wait for me!”
    One boot sits on the left. The right slot is empty, yet the lamp glows green. Duck says, “If the light's on green, my boots are packed.”
    Duck brushes the empty right slot with one wing.
    Duck stares at the single boot and the green light. Duck says, “One boot.”
    Duck lifts the boot. Both slots are empty and the lamp goes dark. Duck says, “It went out. The light's not stuck.”
    The same boot in the right slot turns the lamp green.
    Duck moves the boot back to the left slot. The light is still green.
    Robot identifies the LEFT OR RIGHT rule while one boot keeps the lamp on. Robot says, “Either one of the boots will do to turn on the light. Just one is enough.”
    Robot changes the rule to LEFT AND RIGHT. One boot on the left leaves the lamp dark. Robot says, “The light won't turn on until both boots are in.”
    Duck points at the empty right slot and proposes moving the boot. Duck says, “What if I take that boot and put it on the empty right slot?”
    The right slot now holds the only boot. The AND light stays dark.
    With one boot back on the left, Duck spots another boot outside the case.
    Duck holds the spare boot over the right slot and asks what will happen. Duck says, “What will this light do once I put this boot in?”
    The spare boot hovers above the open slot beside the dark lamp.
    Robot leans forward with both hands poised, waiting for Duck’s test.
    Duck steadies the second boot above the right slot. The lamp is still dark.
    Duck seats the second boot. The lamp glows green.
    Duck lifts the left boot and leaves the right boot in. The light goes dark.
    The left boot goes back in beside the right one, and the lamp turns green. Duck says, “That's what I wanted.”
    Duck holds both boots. The empty case shows a dark lamp.
    Duck pulls on the red boots by the puddle.
    Duck splashes through the parade in both boots while Robot watches. Duck says, “OK, so it's puddle time.”

    Episode transcript

    Scene 1: Wait for me!

    The duck hears the puddle parade. He turns to see what's going on. The duck rushes for his suitcase. The robot in the workshop sees the duck on the monitor.

    The duck opens his suitcase in preparation for the puddle parade. He looks inside. In the left slot he sees a red boot. The right slot is empty. The light for packing is green.

    His wing brushes the bottom of the empty slot.

    Only one boot remains in the left slot.

    Scene 2: The light's not stuck.

    He pulls the one boot out of the left slot. Both slots are now empty. The light goes out.

    The duck inserts that one (and only) boot into the right slot. The light goes green.

    The duck takes that boot out of the right slot and puts it in the left slot. The right slot is now empty and the left slot is the only one with a boot in it. The lamp is green again.

    The rule is: LEFT OR RIGHT. When the left slot is filled with a boot, the light will be on.

    Scene 3: That's what I wanted.

    The robot flips it to the LEFT AND RIGHT rule. He keeps the one boot in the left slot. The other slot is empty. So the light turns off.

    Duck tries out AND by shifting one boot to the right slot of the suitcase. The light does not come on. Just the right boot is there. The left is empty.

    The duck grabs the boot and moves it from the right slot to the left slot in the suitcase. The right slot is now empty again and the light stays off. Then the duck notices the second red boot next to the suitcase.

    The duck is holding the other red boot over the right slot (empty) of the suitcase. The light is off, and the duck pauses.

    The duck stuffs the other red boot in the right slot. The light goes green.

    Duck pulls the boot out of the left slot to test the rule. The boot in the right slot stays in. The left slot is now empty. The light turns off.

    He puts the boot in the left slot of the suitcase. He already has a boot in the right slot. Now both boots are in their slots. The light goes green.

    Scene 4: OK, so it's puddle time.

    The duck is getting the red boots out of the two slots in his suitcase. When there's nothing in the slots the AND light goes out.

    The duck stares at both red boots before he jumps into the puddle.

    Duck splashes through the puddle parade.

    Why the light was wrong

    The two boot slots provide "true" or "false" answers to the program, separately.

    OR means that if one or both answers are yes, turn on the light. AND means if both answers are yes, turn on the light.

    Try all four combinations

    Open MDN Playground (opens in a new tab). Add this code to the JavaScript pane. Run the example. Inspect the 4 rows in the console.

    JavaScript
    const slots = [[false, false], [true, false], [false, true], [true, true]];
    for (const [leftBoot, rightBoot] of slots) {
      const orLight = leftBoot || rightBoot;
      const andLight = leftBoot && rightBoot;
      console.log('Left:', leftBoot, 'Right:', rightBoot, 'OR:', orLight, 'AND:', andLight);
    }

    The only time the two rules give different results is if there's exactly one slot filled.

    Your turn

    A toy lamp needs to switch on only when the switch is on and the plug is in. The switch is on, but the plug is not plugged in. What's the rule? Should the lamp turn on or not?

    Answer

    The lamp doesn't light. The rule for the lamp is that the switch is on AND the plug is in.

    Sources

    MDN: Logical OR (opens in a new tab)

    MDN: Logical AND (opens in a new tab)