Skip to content
Exercise · First program · lesson 7 of 7

Exercise: rectangle perimeter

Write a function function perimeter(width, height) that returns the perimeter of a rectangle with sides width and height. The perimeter is the sum of the lengths of all four sides.

  • perimeter(3, 5) → 16
  • perimeter(1, 1) → 4
  • perimeter(0, 7) → 14

Constraints: the sides are integers from 0 to 1000, and one or both of them may be zero.

The checks are on the perimeter.test.js tab: you can read them but not change them. Each check calls perimeter with the numbers in its name: test_perimeter_3_5 calls perimeter(3, 5) and compares the result with the expected one. The rest of the code in that file comes in later themes.

A failed check prints the line FAIL test_perimeter_3_5 and below it perimeter(3, 5) = -1, want 16. The number after = is what your function returned; the one after want is what was expected. If your code has a syntax error, an error message appears instead of the check results, and no check runs.

Keep the declaration function perimeter(width, height) as it is: the checks call the function by this name and pass it two numbers.

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

perimeter.js — javascript
javascript · 4 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