Today, we are going to write a function called findPerimeter that will accept two integers, length and width, as arguments.

You are given the length and the width of a rectangle. The goal of the function is to output the perimeter of that rectangle. I don’t know when in school you start learning simple geometry but you use this is how you calculate the perimeter of a rectangle:

perimeter = (length + width) * 2
perimeter = length + length + width + width
perimeter = (length * 2) + (width * 2)

All of the examples above are the multiple ways you can get the perimeter of a rectangle. They’re all done differently but they all lead to the same answer.

Now that I helped you re-learn geometry, here are a couple of examples:

findPerimeter(6, 7) ➞ 26
findPerimeter(20, 10) ➞ 60

If you plug in those input values into the perimeter formulas above, your function will output the numbers following the arrow.

Here is a step by step process for the first example.

Length = 6
Width = 7

Perimeter = (6 + 7) * 2
Perimeter = (13) * 2
Perimeter = 26

Now that you understand this, let’s turn this into code.

#programming #coding #javascript

JavaScript Algorithm: Find the Perimeter of a Rectangle
3.00 GEEK