เราต้องเขียนฟังก์ชันที่รับปี (ตัวเลขหรือสตริง) คืนศตวรรษที่ปีนั้นอยู่
ตัวอย่าง: "2000" → 20, 1999 → 20, "2002" → 21
ตัวอย่างโค้ด
const centuryFromYear = year => {
if (typeof year == 'string') {
if (year.toString().slice(-2) == '00') { return year.toString().slice(0,2); }
else { return (Math.floor(+year/100) + 1).toString(); }
} else if (typeof year == 'number') {
return Math.floor((year - 1) / 100) + 1;
} else { return undefined; }
};
console.log(centuryFromYear("2000"));
console.log(centuryFromYear("2002"));
console.log(centuryFromYear(1999));
ผลลัพธ์
ค่าที่แสดงในคอนโซลจะเป็นดังนี้:
20 21 20