Create a function that takes in a year and returns the correct century. All years will be between 1000 and 2010. The 11th century is between 1001 and 1100. The 18th century is between 1701-1800.
Is there a way to do this using regexes? I understand that it's long but I'm just trying to get comfortable with regexes.
function century(num) {
if (/1000/ig.test(num) === true) {
return '10th century';
} else if (/[1001-1100]/ig.test(num) === true) {
return '11th century';
} else if (/[1101-1200]/ig.test(num) === true) {
return '12th century';
} else if (/[1201-1300]/ig.test(num) === true) {
return '13th century';
} else if (/[1301-1400]/ig.test(num) === true) {
return '14th century';
} else if (/[1401-1500]/ig.test(num) === true) {
return '15th century';
} else if (/[1501-1600]/ig.test(num) === true) {
return '16th century';
} else if (/[1601-1700]/ig.test(num) === true) {
return '17th century';
} else if (/[1701-1800]/ig.test(num) === true) {
return '18th century';
} else if (/[1801-1900]/ig.test(num) === true) {
return '19th century';
} else if (/[1901-2000]/ig.test(num) === true) {
return '20th century';
} else if (/[2001-2100]/ig.test(num) === true) {
return '21th century';
} else {
return undefined;
}
}
console.log(century(1756)); //"18th century"
console.log(century(1555)); //"16th century"
console.log(century(1000)); //"10th century"
console.log(century(1001)); //"11th century"
console.log(century(2005)); //"21th century"