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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP