猿问

以数字字为参数的 parseInt 函数

我需要一些帮助,我的代码适用于三位数字,我需要这个与七位数字一起使用。首先拆分输入,然后迭代并与数据库变量进行比较..我真的停留在这一点上。输入可能包含也可能不包含连字符。提前致谢。


function parseInt(string) {

  var output = [];

  var dataBase = {

    'zero': 0,

    'one': 1,

    'two': 2,

    'three': 3,

    'four': 4,

    'five': 5,

    'six': 6,

    'seven': 7,

    'eight': 8,

    'nine': 9,

    'ten': 10,

    'eleven': 11,

    'twelve': 12,

    'thirteen': 13,

    'fourteen': 14,

    'fifteen': 15,

    'sixteen': 16,

    'seventeen': 17,

    'eighteen': 18,

    'nineteen': 19,

    'twenty': 20,

    'thirty': 30,

    'forty': 40,

    'fifty': 50,

    'sixty': 60,

    'seventy': 70,

    'eighty': 80,

    'ninety': 90,

    'hundred': 100,

    'thousand': 1000,

    'million': 1000000,

    'and': 0

  }


  var arr = string.split(" ");

  console.log(arr);


  var length = arr.length;


  for (i = 0; i < length; i++) {

    if (arr[i].includes("-")) {

      var index = arr.indexOf(arr[i]);

      var hecta = arr[i].split('-');

      arr.splice(index, 1, hecta[0], hecta[1]);

    }

  }


  console.log(arr);


  for (i = 0; i < arr.length; i++) {

    output.push(dataBase[arr[i]]);

  }


  console.log(output);


  var transform = 0;

  transform = output[0] * output[1] + output[2] + output[3];

  return transform;

}


console.log(parseInt('two hundred forty-six')); //246

console.log(parseInt("one hundred seventy-one")); //171

console.log(parseInt('one thousand two hundred and thirty')); // not works


慕后森
浏览 92回答 3
3回答

LEATH

在阅读了mlibby的评论(以百万为单位的值)之后,我决定从头开始。结果真的很简单!我还添加了一个界面以更易读的方式显示结果const dataBase =&nbsp;&nbsp; &nbsp; &nbsp; { zero: 0, one: 1, two: 2, three: 3, four: 4&nbsp; &nbsp; &nbsp; , five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10&nbsp; &nbsp; &nbsp; , eleven: 11, twelve: 12, thirteen: 13, fourteen: 14&nbsp; &nbsp; &nbsp; , fifteen: 15, sixteen: 16, seventeen: 17, eighteen: 18&nbsp; &nbsp; &nbsp; , nineteen: 19, twenty: 20, thirty: 30, forty: 40&nbsp; &nbsp; &nbsp; , fifty: 50, sixty: 60, seventy: 70, eighty: 80, ninety: 90&nbsp; &nbsp; &nbsp; , hundred: 100, thousand: 1000, million: 1000000, and: 0&nbsp; &nbsp; &nbsp; }&nbsp;const parseVal=s=>&nbsp; &nbsp; &nbsp; &nbsp; s.match(/\w+/g)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.reduce((tots,sVal)=>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let n = tots.length -1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (sVal) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'hundred':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tots[n] *= 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'thousand':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'million':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tots[n] *= dataBase[sVal]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tots.push(0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tots[n] += dataBase[sVal]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return tots&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },[0]).reduce((a,c)=>a+c);// testing part...const myTable = document.getElementById('my-table')for(let r=1;r<myTable.rows.length;++r )&nbsp; {&nbsp; let rowCell = myTable.rows[r].cells&nbsp; rowCell[1].textContent = parseVal(rowCell[0].textContent).toLocaleString('en')&nbsp; if (rowCell[1].textContent === rowCell[3].textContent)&nbsp; &nbsp; {&nbsp; &nbsp; rowCell[2].textContent = 'OK'&nbsp; &nbsp; rowCell[2].className = 'isOK'&nbsp; &nbsp; }&nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; rowCell[2].textContent = 'bad'&nbsp; &nbsp; rowCell[2].className = 'isBad'&nbsp; &nbsp; }&nbsp; }table {&nbsp; border-collapse: collapse;&nbsp; margin-top: 25px;&nbsp; font-family: Arial, Helvetica, sans-serif;&nbsp; font-size: 12px;&nbsp; }thead {&nbsp; background-color: aquamarine;&nbsp; }tbody {&nbsp; background-color: #e1e7ee;&nbsp; }td {&nbsp; border: 1px solid grey;&nbsp; padding: .3em .7em;&nbsp; }td:nth-child(2),td:nth-child(4) {&nbsp; text-align: right;&nbsp; }td:nth-child(3) { text-align: center; }td.isOK { color:green }td.isBad { color:red }<table id="my-table">&nbsp; <thead>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>text value</td> <td>Parsed</td> <td>validation</td><td>expected</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; </thead>&nbsp; <tbody>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>two hundred forty-six</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>246</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>two hundred thousand</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>200,000</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>two hundred thousand and twelve</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>200,012</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>one hundred seventy-one</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>171</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>one thousand two hundred and thirty</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>1,230</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td>three hundred and twenty one million four hundred thirty three thousand seven hundred and eight</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>...</td>&nbsp; &nbsp; &nbsp; <td>321,433,708</td>&nbsp;&nbsp; &nbsp; </tr>&nbsp; </tbody></table>

眼眸繁星

正如我在评论中指出的那样,您的转换没有做您想要的,您需要反向迭代整个数组以使用您当前的技术。所以我重构了你的代码来演示。我改变了什么?我用我将使用的名称替换了变量名称(命名是主观的,但我更喜欢它们是描述性的)。下一步: String.split() 可以采用正则表达式,并且能够一次拆分多个不同的字符。正则表达式/[ -]/仅表示在空格或破折号的任何字符上拆分。然后: Array.map() 允许我们通过使用指定的函数转换每个元素来从现有数组创建一个新数组(在这种情况下,该函数正在从您的单词“数据库”中查找整数)。最后,我们遍历数字数组,将它们适当地添加到最终总和中。function parseInt(string) {&nbsp; &nbsp; const wordNumbers = {&nbsp; &nbsp; &nbsp; 'zero': 0,&nbsp; &nbsp; &nbsp; 'one': 1,&nbsp; &nbsp; &nbsp; 'two': 2,&nbsp; &nbsp; &nbsp; 'three': 3,&nbsp; &nbsp; &nbsp; 'four': 4,&nbsp; &nbsp; &nbsp; 'five': 5,&nbsp; &nbsp; &nbsp; 'six': 6,&nbsp; &nbsp; &nbsp; 'seven': 7,&nbsp; &nbsp; &nbsp; 'eight': 8,&nbsp; &nbsp; &nbsp; 'nine': 9,&nbsp; &nbsp; &nbsp; 'ten': 10,&nbsp; &nbsp; &nbsp; 'eleven': 11,&nbsp; &nbsp; &nbsp; 'twelve': 12,&nbsp; &nbsp; &nbsp; 'thirteen': 13,&nbsp; &nbsp; &nbsp; 'fourteen': 14,&nbsp; &nbsp; &nbsp; 'fifteen': 15,&nbsp; &nbsp; &nbsp; 'sixteen': 16,&nbsp; &nbsp; &nbsp; 'seventeen': 17,&nbsp; &nbsp; &nbsp; 'eighteen': 18,&nbsp; &nbsp; &nbsp; 'nineteen': 19,&nbsp; &nbsp; &nbsp; 'twenty': 20,&nbsp; &nbsp; &nbsp; 'thirty': 30,&nbsp; &nbsp; &nbsp; 'forty': 40,&nbsp; &nbsp; &nbsp; 'fifty': 50,&nbsp; &nbsp; &nbsp; 'sixty': 60,&nbsp; &nbsp; &nbsp; 'seventy': 70,&nbsp; &nbsp; &nbsp; 'eighty': 80,&nbsp; &nbsp; &nbsp; 'ninety': 90,&nbsp; &nbsp; &nbsp; 'hundred': 100,&nbsp; &nbsp; &nbsp; 'thousand': 1000,&nbsp; &nbsp; &nbsp; 'million': 1000000,&nbsp; &nbsp; &nbsp; 'and': 0&nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; let words = string.split(/[ -]/);&nbsp; &nbsp; console.log(words);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; let numbers = words.map(word => wordNumbers[word]);&nbsp; &nbsp; numbers.reverse();&nbsp; &nbsp; console.log(numbers);&nbsp;&nbsp;&nbsp; &nbsp; let result = 0;&nbsp; &nbsp; for(let i = 0; i < numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if(numbers[i] < 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += numbers[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i + 2 < numbers.length && numbers[i + 1] < numbers[i] && numbers[i + 2] < numbers [i + 1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += numbers[i] * numbers[i + 1] * numbers[i + 2];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++; i++;// need to skip the next two elements of ints in the for loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(i + 1 < numbers.length && numbers[i + 1] < numbers[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += numbers[i] * numbers[i + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++; // need to skip the next element of ints in the for loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result += numbers[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;&nbsp; }&nbsp;&nbsp;&nbsp; console.log(parseInt('two hundred forty-six')); //246&nbsp; console.log(parseInt("one hundred seventy-one")); //171&nbsp; console.log(parseInt('one thousand two hundred and thirty')); // 1230&nbsp; console.log(parseInt('twenty million')); // 20000000&nbsp; console.log(parseInt('two hundred thousand')); // 200000

九州编程

这适用于诸如二十万之类的条目...function parseInt(string) {&nbsp; var output = [];&nbsp; var dataBase = {&nbsp; &nbsp; 'zero': 0,&nbsp; &nbsp; 'one': 1,&nbsp; &nbsp; 'two': 2,&nbsp; &nbsp; 'three': 3,&nbsp; &nbsp; 'four': 4,&nbsp; &nbsp; 'five': 5,&nbsp; &nbsp; 'six': 6,&nbsp; &nbsp; 'seven': 7,&nbsp; &nbsp; 'eight': 8,&nbsp; &nbsp; 'nine': 9,&nbsp; &nbsp; 'ten': 10,&nbsp; &nbsp; 'eleven': 11,&nbsp; &nbsp; 'twelve': 12,&nbsp; &nbsp; 'thirteen': 13,&nbsp; &nbsp; 'fourteen': 14,&nbsp; &nbsp; 'fifteen': 15,&nbsp; &nbsp; 'sixteen': 16,&nbsp; &nbsp; 'seventeen': 17,&nbsp; &nbsp; 'eighteen': 18,&nbsp; &nbsp; 'nineteen': 19,&nbsp; &nbsp; 'twenty': 20,&nbsp; &nbsp; 'thirty': 30,&nbsp; &nbsp; 'forty': 40,&nbsp; &nbsp; 'fifty': 50,&nbsp; &nbsp; 'sixty': 60,&nbsp; &nbsp; 'seventy': 70,&nbsp; &nbsp; 'eighty': 80,&nbsp; &nbsp; 'ninety': 90,&nbsp; &nbsp; 'hundred': 100,&nbsp; &nbsp; 'thousand': 1000,&nbsp; &nbsp; 'million': 1000000,&nbsp; &nbsp; 'and': 0&nbsp; }&nbsp; &nbsp;let clarray=[]&nbsp; &nbsp;filteroutp=[]&nbsp; &nbsp;fo=[]&nbsp; &nbsp;var f&nbsp; &nbsp;var acc=[]&nbsp; &nbsp;var res=0&nbsp; &nbsp; const strarray= string.split(' ')&nbsp; &nbsp; strarray.forEach((n,i)=>{&nbsp; &nbsp; &nbsp; if(n.includes('-')) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; nm=n.split('-'), clarray.push(...nm)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; else clarray.push(n)&nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; Object.entries(dataBase).forEach(o=>{&nbsp; &nbsp; clarray.forEach((x,i)=>{if (x==o[0]) output[i]=o[1] })})&nbsp; &nbsp; &nbsp;if(output[output.length-1]&&output[output.length-2]<100&&output.length-2!=0){&nbsp; &nbsp; &nbsp; n=output.pop() , f=output.pop()+n&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;else f=0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(let i=0;i<output.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n=output[i]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m= output[i+1]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(m=="undefined") m=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(output.length>1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n<m ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(acc.length!=0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accn= acc.shift()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accn<n? fo.push(accn*n*m):fo.push(accn+n*m)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.splice(i,2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=-2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if(acc.length==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acc.push(n*m)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(output.length>1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;output.splice(i,2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i=-2&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(output.length==1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fo.push(acc.shift()*output[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.splice(0,1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i=-1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(output.length==0&&acc.length!=0) fo.push(acc.shift())&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; fo.push(f)&nbsp; &nbsp; &nbsp; for(let n of fo){&nbsp; &nbsp; &nbsp; &nbsp; res+=n&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; return "output "+`${fo}` + " number "+res}console.log(parseInt('two hundred forty-six')); //246console.log(parseInt("one hundred seventy-one")); //171console.log(parseInt('one thousand two hundred and thirty')); //1230console.log(parseInt('one million one thousand two hundred and thirty')); // 1001230console.log(parseInt('ten million one-thousand two hundred and thirty-two')); // 10001232console.log(parseInt('one thousand seventeen hundred and thirty')); // 2730console.log(parseInt('two hundred thousand')) //200000
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答