Setup
Americans can be weird sometimes. The first floor of a building is typically referred to as the ground floor. In some American buildings, there is no 13th floor because of old superstitions.
Implement a function that takes an American floor passed as an integer and returns the actual floor number. Your function should also work for basement floors.
Examples
getRealFloor(1) == 0
getRealFloor(0) == 0
getRealFloor(5) == 4
getRealFloor(15) == 13
getRealFloor(-3) == -3
Tests
getRealFloor(3)
getRealFloor(7)
getRealFloor(20)
getRealFloor(1)
getRealFloor(-6)
Good luck!
This challenge comes from acadet 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!
Top comments (9)
Elixir
I wasn't sure if the 13th floor thingie was also supposed to apply to basement levels. I wrote a solution that does it.
Elm
C#
Same as @savagepixie , not sure about negative superstitions, though I opted not to include it.
C++ one liner
Here is what that thing actually is
Python
Ruby Oneliner
Some tests
In JavaScript
Rust pattern matching
return floor - (floor < 1 ? 0 : floor < 13 ? 1 : 2);