## DEV Community is a community of 891,187 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

dev.to staff

Posted on

# Daily Challenge #281 - Area or Perimeter

You are given the length and width of a 4-sided polygon. The polygon can either be a rectangle or a square.

If it is a square, return its area. If it is a rectangle, return its perimeter.

`area_or_perimeter(6, 10)` --> 32
`area_or_perimeter(4, 4)` --> 16

### Tests

`area_or_perimeter(5, 5)`
`area_or_perimeter(10, 20)`

Good luck!

This challenge comes from `no one` on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion (11)

Sergiu Pruteanu

learning Go

``````func area_or_perimeter(x int, y int) int {
if x == y {
return x * y
} else {
return 2 * (x + y)
}
}

func main() {
//tests
fmt.Println(area_or_perimeter(5, 5))
fmt.Println(area_or_perimeter(10, 20))
}
``````
Mihail Malo
``````        mov     rcx, rsi
imul    rcx, rdi
lea     rax, [rsi + rdi]
cmp     rdi, rsi
cmove   rax, rcx
ret
``````

ooga booga

Deepak Raj
``````def area_or_perimeter(a, b):
if a == b:
return a * b
else:
return 2 * (a + b)

assert area_or_perimeter(5, 5) == 25
assert area_or_perimeter(10, 20) == 60
``````
Rafael Acioly
``````return a * b if a == b else 2 * (a + b)
``````

:)

peter279k

Here is the simple solution with Python:

``````def area_or_perimeter(l , w):
if l == w:
return l * w
return (l + w) * 2
``````
Hridayesh Sharma • Edited on
``````const area_or_perimeter = (a,b) => a===b ? a*b : 2*(a+b);

``````
Gagan
``````def area_or_perimeter(l,w):
return l*w if l==w else 2*(l+w)
``````
Alexander Girke

In Kotlin it would be:

``````fun area_or_perimeter(a: Int, b: Int) =
if (a == b)
a * b
else
2 * (a + b)
``````
bidetaggle • Edited on

In js: `let area_or_perimeter = (x, y) => x === y ? x*y : 2*(x+y);`

Pass all the tests.

Michael Kohl

A solution in Forth:

``````: area-or-perimeter 2dup = if * else + 2 * then ;
4 4 area-or-perimeter . 16  ok
6 10 area-or-perimeter . 32  ok
``````
Hasobi

It's quite simple, check whether a=b if it's true return a*b if it's not, return (a+b)*2.