gpt4 book ai didi

javascript - JavaScript 单词到数字转换器中缺少空格

转载 作者:行者123 更新时间:2023-11-28 13:51:09 25 4
gpt4 key购买 nike

我已经创建了一个 0-999 的数字到单词转换器,除了一个错误之外,它几乎可以完美运行。

在超过一百的某些数字中,“百”部分和“十/单位”部分之间缺少空格。

例如 864 给出“eth canspajar ha tri ugens”,其中 jar 头和 pajar 之间需要有一个空格(百 = jar 头)

这似乎是一个常见问题,因为它发生在 164、264、364 等以及其他数字范围内。

任何对 Javascript 或数学有更好了解的人都可以看一下我的代码,看看他们是否可以发现其中的模式或解决方案吗?该代码取自一个法语数字转换器,该转换器不是我制作的,而且我对 Javascript 还比较陌生,因此要完全理解其中的所有内容对我来说很困难。

您可以在下面或http://jsfiddle.net/nickykw/tMXmj/2/处看到代码

非常感谢

    function num2Letters(number) {

if (isNaN(number) || number < 0 || 999 < number) {
return 'Veuillez entrer un nombre entier compris entre 0 et 999.';
}

var units2Letters = ['', 'onen', 'dew', 'tri', 'pajar', 'pemp', 'whegh', 'seyth', 'eth', 'naw', 'deg', 'udnek', 'dowdhek', 'tredhek', 'peswardhek', 'pemthek', 'whetek', 'seytek', 'etek', 'nownsek'],
tens2Letters = ['', 'deg', 'ugens', 'warn ugens', 'dew ugens', 'ha dew ugens', 'tri ugens', 'ha tri ugens', 'pajar ugens', 'ha pajar ugens'];
twenties2Letters = ['', 'deg', 'ugens', 'warn ugens', 'dew ugens', 'ha dew ugens', 'tri ugens', 'ha tri ugens', 'pajar ugens', 'ha pajar ugens', 'pemp ugens', 'ha pemp ugens', 'whegh ugens', 'ha whegh ugens', 'seyth ugens', 'ha seyth ugens', 'eth ugens', 'hag eth ugens', 'naw ugens', 'ha naw ugens'];
hundreds2Letters = ['', 'cans', 'dew cans', 'tri hans', 'pajar cans', 'pemp cans', 'whegh cans', 'seyth cans', 'eth cans', 'naw cans'];

var units = number % 10,
tens = (number % 100 - units) / 10,
twenties = (number % 200 - units) / 10,
hundreds = (number % 1000 - number % 100) / 100;

var unitsOut, tensOut, twentiesOut, hundredsOut;


if (number === 0) {

return 'mann';

} else {

// THE UNITS

unitsOut = units2Letters[units];

// THE TENS

if (tens === 1 && units > 0) {

tensOut = units2Letters[10 + units];
unitsOut = '';

} else if (tens === 2 && units !== 0 ) {

tensOut = units2Letters[units] + ' warn ' + tens2Letters[tens];
unitsOut = '';

} else if (tens === 3) {

tensOut = units2Letters[10 + units] + ' ' + tens2Letters[tens];
unitsOut = '';


} else if ((tens === 4 && units !== 0) || (tens === 6 && units !== 0) || (tens === 8 && units !== 0)) {

tensOut = units2Letters[units] + ' ha ' + tens2Letters[tens];
unitsOut = '';

} else if (tens === 5 || tens === 7 || tens === 9) {

tensOut = units2Letters[10 + units] + ' ' + tens2Letters[tens];
unitsOut = '';

} else {

tensOut = tens2Letters[tens];

}





// THE TWENTIES - used only for 120-199

if ((number >= 121 && number <= 199) && (tens === 2 || tens === 4 || tens === 8) && units > 0) {

twentiesOut = units2Letters[units] + ' ha ' + twenties2Letters[twenties];

} else if ((number >= 121 && number <= 199) && (tens === 3 || tens === 5 || tens === 7 || tens === 9) ) {

twentiesOut = units2Letters[10 + units] + ' ' + twenties2Letters[twenties];

} else if ((number >= 121 && number <= 199) && (tens === 6 ) ) {

twentiesOut = units2Letters[units] + ' hag ' + twenties2Letters[twenties];

} else {

twentiesOut = twenties2Letters[twenties];

}







// THE HUNDREDS

//if the number is x01-09 or number is x1x or number is x2x or number is x3x then add a hag/ha after the number (hag if the number is xx1 or xx8 or x20)
if ((hundreds >= 1 && tens === 0 && units >= 1) || hundreds >= 1 && tens === 1 || hundreds >= 1 && tens === 2 || hundreds >= 1 && tens === 3) {

hundredsOut = hundreds2Letters[hundreds] + (hundreds >= 1 && (units === 1 || units === 8) || hundreds >= 1 && tens === 2 && units === 0 ? ' hag ' : ' ha ');


// insert a ha for 40, 60 and 80 above 200
} else if (hundreds >= 2 && (tens === 4 || tens === 6 || tens === 8) && units === 0) {

hundredsOut = hundreds2Letters[hundreds] + ' ha ';

} else {

hundredsOut = hundreds2Letters[hundreds];

}


// GET TOTAL

if (number === 50) {

return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> hanter cans';

} else if (number === 150) {

return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> deg ha seyth ugens <i>or</i> onen cans ha hanter';

} else if (hundreds > 1 && tens === 5 && units === 0 ) {

return hundredsOut + (hundredsOut && tensOut ? ' ': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut + ' <i>or</i> ' + hundredsOut + ' ha hanter';

} else if (hundreds === 1 && (tens === 2 || tens === 3 || tens === 4 || tens === 5 || tens === 6 || tens === 7 || tens === 8 || tens === 9) ) {

return twentiesOut + ' <i>or</i> ' + hundredsOut + (hundredsOut && tensOut ? '': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut;

} else {

return hundredsOut + (hundredsOut && tensOut ? '': '') + tensOut + (hundredsOut && unitsOut || tensOut && unitsOut ? '' : '') + unitsOut;

}

}

}



var userEntry;

while (userEntry = prompt('Enter a number between 0 and 999:')) {

alert(num2Letters(parseInt(userEntry, 10)));

}

最佳答案

与其试图找到缺失的空间,我建议您重新构建代码,这样您就不会动态地组合字符串,而是将短语的组成部分添加到数组中(使用 push 或根据需要concat),然后在数组上调用join

例如

var result = ['eth','cans'];
result.push('pajar');
result = result.concat(['ha','tri','ugens']);
alert(result.join(' '));

这里的优点是您的代码更加干净,并且您不必担心 trim 可能在某些输入中出现的前导和尾随空格。

关于javascript - JavaScript 单词到数字转换器中缺少空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10872586/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com