问题:给定10-40范围内的整数,该整数确定某个主题的训练分配数。输出指定数量的任务的字符串描述,确保数字与单词"学习作业"的正确一致,例如:18 - "十八个学习作业",23 - "二十三个学习作业",31 - "三十一个学习作业"。
我写了类似的东西,var ten,
set,
unit;
switch(ten){
case 11:
return "eleven";
break;
case 12:
return "twelve";
break;
case 13:
return "thirteen";
break;
case 14:
return "fourteen";
break;
case 15:
return "fifteen";
break;
case 16:
return "sixteen";
break;
case 17:
return "seventeen";
break;
case 18:
return "eighteen";
break;
case 19:
return "nineteen";
break;
}
if (ten<9) {
return "Error number is less than 9";
}
switch(set){
case 10:
return "ten";
break;
case 20:
return "twenty";
break;
case 30:
return "thirty";
break;
case 40:
return "fourty";
break;
}
if(set<9) {
return "Error number is less than 9";
}
switch (unit)
{
case 1:
return "on1";
break;
case 2:
return "two";
break;
case 3:
return "three";
break;
case 4:
return "four";
break;
case 5:
return "five";
break;
case 6:
return "six";
break;
case 7:
return "seven";
break;
case 8:
return "eight";
break;
case 9:
return "nine";
break;
}
任何帮助将不胜感激!
答案 0 :(得分:0)
如果您的数字范围有限并且想要一个没有任何库的简单解决方案,您可以尝试简单的映射:
var numbers = {
'10': 'ten',
'11': 'eleven',
'12': 'twelve',
'13': 'thirteen'
// ...
};
console.log(numbers['11']); // 'eleven'
否则,已经提到的JavaScript numbers to Words似乎是一个很好的解决方案。