Q:

Write a JavaScript program to add an ordinal suffix to a number

0

Write a JavaScript program to add an ordinal suffix to a number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

const toOrdinalSuffix = num => {
  const int = parseInt(num),
    digits = [int % 10, int % 100],
    ordinals = ['st', 'nd', 'rd', 'th'],
    oPattern = [1, 2, 3, 4],
    tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19];
  return oPattern.includes(digits[0]) && !tPattern.includes(digits[1])
    ? int + ordinals[digits[0] - 1]
    : int + ordinals[3];
};

console.log(toOrdinalSuffix('1')); 
console.log(toOrdinalSuffix('4')); 
console.log(toOrdinalSuffix('50')); 
console.log(toOrdinalSuffix('123'));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now