## DEV Community is a community of 890,178 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Daily Challenge #54 - What century is it?

Cagatay Kaya • Edited on

A length Javascript solution, but I did not want to divide by 100.

``````const century = year => {
const yearString = year.toString();
const n = String(year).length;
if (n - 3 < 0) {
console.log("0th century");
} else {
console.log(digitYears(yearString, n));
}
};

const digitYears = (yearString, n) => {
const toIntAgain = parseInt(yearString[n - 3]) + 1;
const edges = parseInt(yearString.slice(n - 4, n - 2)) + 1;
if (edges == 11 || edges == 12 || edges == 13) {
return `\${yearString.slice(0, n - 3)}\${toIntAgain}th century`;
} else {
const ending = endingDetermine(toIntAgain);
return `\${yearString.slice(0, n - 3)}\${toIntAgain}\${ending} century`;
}
};

const endingDetermine = digit => {
let ending = "";
switch (digit) {
case 1:
ending = "st";
break;
case 2:
ending = "nd";
break;
case 3:
ending = "rd";
break;
default:
ending = "th";
break;
}
return ending;
};
``````

Tried it with a few different years including the edge cases.

``````century(11034); //111th century
century(15134); //152nd century
century(16234); //163th century
century(942); //10th century
century(2042); //21st century
century(1342); //14th century
century(1242); //13th century
century(52); //0th century
``````