JavaScript 中单词的数字和操作数
问题
我们需要编写一个JavaScript函数,它接受一些数学运算的字符串并返回其字面意思。
示例
以下是代码-
const str = '5 - 8';
const convertToWords = (str = '') => {
const o = {
"+" : "Plus",
"-" : "Minus",
"*" : "Times",
"/" : "Divided By",
"**" : "To The Power Of",
"=" : "Equals",
"!=" : "Does Not Equal",
}
const n = {
1 : "One",
2 : "Two",
3 : "Three",
4 : "Four",
5 : "Five",
6 : "Six",
7 : "Seven",
8 : "Eight",
9 : "Nine",
10 : "Ten",
}
let t = str.split(' ')
let y = ''
let c = 0
for (const [key, value] of Object.entries(o)) {
if(key !== t[1])
c++;
}
if(c === Object.keys(o).length) return "That\'s not an operator!"
for (const [key, value] of Object.entries(n)) {
if(key === t[0])
y += `${value} `
}
for (const [key, value] of Object.entries(o)) {
if(key === t[1])
y += `${value}`
}
for (const [key, value] of Object.entries(n)) {
if(key === t[2])
y += ` ${value}`
}
return y;
}
console.log(convertToWords(str));输出结果Five Minus Eight
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短