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 次瀏覽

開啟你的職業生涯

完成課程後獲得認證

開始
廣告