programming-examples/js/String/Get humanized number with the correct suffix.js

15 lines
519 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function humanize(number) {
if(number % 100 >= 11 && number % 100 <= 13)
return number + "th";
switch(number % 10) {
case 1: return number + "st";
case 2: return number + "nd";
case 3: return number + "rd";
}
return number + "th";
}
console.log(humanize(1));
console.log(humanize(20));
console.log(humanize(302));