Last active
March 16, 2017 16:20
-
-
Save devNoiseConsulting/1cce8ca22a2204f1dec5eb084cedb504 to your computer and use it in GitHub Desktop.
Ordinal Numbers - PhillyDev Slack #daily_programmer - 20170228
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Today's daily programmer challenge is to print a marathon finishers | |
place in English. Your program should take an non decimal input | |
`getFinishPosition(11)` and return `11th place`, `getFinishPosition(21)` | |
and return `21st place`, `getFinishPosition(4)` and return `4th place`, | |
and `getFinishPosition(33)` and return `33rd place`. | |
Please DM me with ideas for future problems. When you have completed it | |
post a link to your solution. No need to go over 99 finishers, unless | |
you want to. | |
*/ | |
function getFinishPosition_1(place) { | |
let digit = place % 100; | |
if (place > 3 && place < 14) { | |
return place + "th place"; | |
} | |
digit = place % 10; | |
let placement = place; | |
switch (digit) { | |
case 1: | |
placement += "st place"; | |
break; | |
case 2: | |
placement += "nd place"; | |
break; | |
case 3: | |
placement += "rd place"; | |
break; | |
default: | |
placement += "th place"; | |
} | |
return placement; | |
} | |
function getFinishPosition_2(place) { | |
let placement = place.toString(); | |
let lastDigit = parseInt(placement.slice(-1)); | |
let oridinalSuffix = ['th place', 'st place', 'nd place', 'rd place']; | |
let specialCase = placement.match(/^([0-9]+?[2-90][123]|[123])$/); | |
if (specialCase) { | |
console.log(specialCase); | |
placement += oridinalSuffix[lastDigit]; | |
} else { | |
placement += oridinalSuffix[0]; | |
} | |
return placement; | |
} | |
let getFinishPosition = getFinishPosition_1; | |
console.log(getFinishPosition(11)); | |
console.log(getFinishPosition(21)); | |
console.log(getFinishPosition(4)); | |
console.log(getFinishPosition(33)); | |
for (let i = 110; i < 115; i++) { | |
console.log(getFinishPosition(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment