从JavaScript中获得世纪
我们需要编写一个JavaScript函数,该函数接受一个代表年份的数字或字符串。从那一年起,我们的职能应该弄清楚并返回这一年的世纪。
例如-
f("2000") = 20
f(1999) = 20
f("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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短