DEV Community

Discussion on: Daily Challenge #296 - Years to Centuries

Collapse
 
mellen profile image
Matt Ellen
function thuffix(n)
{
  let ending = 'th';
  if(n%100 != 11 && n%100 != 12 && n%100 != 13)
  {
    if(n%10 == 1)
    {
      ending = 'st';
    }
    else if(n%10 == 2)
    {
      ending = 'nd';
    }
    else if(n%10 == 3)
    {
      ending = 'rd';
    }
  }
  return ending;
}

function getCentury(year)
{
  let c = Math.floor(year/100);
  if(year%100 != 0)
  {
    c++;
  }
  return `${c}${thuffix(c)}`;
}