JavaScript 获取英文计数数字


我们需要编写一个 JavaScript 函数,该函数接受一个数字,并返回其英文计数数字。

例如 

3 returns 3rd

这样做的代码如下 −

const num = 3;
const englishCount = num => {
   if (num % 10 === 1 && num % 100 !== 11){
      return num + "st";
   };
   if (num % 10 === 2 && num % 100 !== 12) {
      return num + "nd";
   };
   if (num % 10 === 3 && num % 100 !== 13) {
      return num + "rd";
   };
   return num + "th";
};
console.log(englishCount(num));
console.log(englishCount(111));
console.log(englishCount(65));
console.log(englishCount(767));

以下是控制台上的输出 −

3rd
111th
65th
767th

更新于: 09-10-2020

121 次浏览

开启你的职业生涯

完成课程后获得认证

开始
广告
© . All rights reserved.