Skip to content

💻 Coding & Logic

Code a smart thermostat

Write real JavaScript for a heating and cooling thermostat, then run automatic tests to prove it works, just like professional programmers.

⏱️ 40 min🎂 Ages 13–15📈 hard
  • Computational thinking
  • Problem solving
  • Critical thinking
  • Measuring & data

🧰 You will need

  • 1 × Kitchen thermometer
  • 1 × Pencils and crayons

🌡️ A tiny program that keeps you comfy

A smart thermostat reads the temperature and the time, then decides: heat, cool or off. That decision is a few lines of code, and today you write it, in JavaScript, a real programming language used on almost every website.

🎮 Write the code

Read the task, change the code, and press Run tests. Each test gives your function a temperature and an hour, and checks the answer. Green ✅ for every row moves you up a level. Stuck? Read the ❌ rows: what did your code say, and what should it have said?

Level 1: Heat when cold

Return "heat" when the temperature is below 18, otherwise "off". (The starter code already does this. Press Run tests!)

Tests
temphourwantgot
12°12:00heat
17°12:00heat
18°12:00off
22°12:00off

Edit the code, then run the tests.

Tips:

  • if (temp > 26) { return "cool"; } checks a condition.
  • && means AND, || means OR.
  • The order of your ifs matters: the first return that runs wins.
  • Your code runs in a safe box in your browser. It can’t break anything, and it stops after 1 second if it gets stuck in a loop.

🛠️ Test it in the real world

  1. Measure the temperature in 4 places at home: a sunny window, the fridge door (outside!), a bedroom, near the kitchen.
  2. Write down the time of each reading.
  3. For each reading, work out by hand what your code would decide. Is it what you would want?
  4. Find a reading where the code does something annoying. Change a rule (and the tests!) to fix it.

💡 What happened?

You wrote a function: a named piece of code that takes inputs (temp, hour) and gives back an output. The tests are a list of inputs and the outputs we expect. Professional programmers write tests so that when they change code later, they know straight away if something broke. When a test failed, you found the bug by comparing what you wanted with what you got. That’s debugging.

Did it work?