Skip to content

Instantly share code, notes, and snippets.

@a-eid
Created July 3, 2017 19:36
Show Gist options
  • Save a-eid/a36f8eb70f8046622f893e19a75548f2 to your computer and use it in GitHub Desktop.
Save a-eid/a36f8eb70f8046622f893e19a75548f2 to your computer and use it in GitHub Desktop.
/* takes single number and convert it to a position
@arg:
Int: number
@returns:
String: position (ie: 1st , 2nd , 3rd , 4th ,... 101st , 1004th ...)
*/
var calcPosition = (rank) => {
let final
let length = rank.toString().length
--length
if (length >= 1) {
let z = "0"
for (i = 1; i < length; i++)
z += "0"
let d = "1" + z
console.log("d " , d )
final = rank % parseInt(d)
} else {
final = rank
}
switch (final) {
case 1:
rank = rank + "st"; break
case 2:
rank = rank + "nd"; break
case 3:
rank = rank + "rd"; break
default:
rank = rank + "th";
}
return rank
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment