Skip to content
Exercise · Classes and prototypes · lesson 11 of 12

Exercise: temperature

Complete the class Temperature. Write constructor(celsius), which stores the temperature in degrees Celsius in an ordinary property celsius. The getter and setter fahrenheit convert it to degrees Fahrenheit and back by the formulas F = C × 1.8 + 32 and C = (F − 32) / 1.8. The function function createTemperature(celsius) in the starter code is already finished and must stay: the tests create objects through it.

  • createTemperature(100).fahrenheit → 212
  • t = createTemperature(0), then t.fahrenheit = 212 → t.celsius is 100
  • t = createTemperature(0), then t.celsius = 37 → t.fahrenheit is 98.6

Constraints: temperatures are numbers, whole or fractional, possibly negative; a fractional result is compared to within 0.000000001; celsius may change from outside, and fahrenheit always matches it.

Do not add extra imports unless the task says so explicitly.

temperature.js — javascript
javascript · 13 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way