JavaScript 获取英文计数数字

javascriptweb developmentobject oriented programmingfront end technology更新于 2026/1/16 17:48:17

我们需要编写一个 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

相关文章


有用资源