Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript parse string to integer
    text
    copied!<p>Working over at codewars I was trying to solve this problem:</p> <p>In this kata we want to convert a string into an integer. The strings simply represent the numbers in words.</p> <p>Examples:</p> <ul> <li>"one" => 1</li> <li>"twenty" => 20</li> <li>"two hundred forty-six" => 246</li> <li>"seven hundred eighty-three thousand nine hundred and nineteen" => 783919</li> </ul> <p>==================================================================================</p> <p>I came up with the code below to do this. <a href="http://jsfiddle.net/eaRGG/" rel="noreferrer">On jsfiddle for you convience.</a></p> <p><strong><em>A problem I've run into is 'seven hundred thousand' gives you 10700.</em></strong></p> <p>I've spent a day looking around and trying to figure this out but am just flat stuck. The steps the program takes is:</p> <ul> <li>string becomes 'thousand hundred seven' - good</li> <li>first while loop finds 'thousand' and sets multiplier to 1000 - good</li> <li>second while loop finds 'hundred' but then the mult.exec(a[0]) if statement resolves to null. - damn</li> </ul> <p>So instead of the multiplier becoming 100000, the value becomes 100000, and we are doomed to get the wrong answer.</p> <p>While trying to debug this I tried creating the a array being used during the second loop in the while in jsfiddle. There it worked and equated to 'hundred' instead of null. Anyone know why this would happen? </p> <pre><code>function parseInt(number) { // reference array for english -&gt; integer var ref = { 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 }; // regex to find number values from the string var find = new RegExp( "(one|t(wo|hree|en|welve|hirteen|wenty|hirty)|f(our|ive|ourteen|iftenn|orty|ifty)|s(ixteen|ixty|eventy|ix|even|eventeen|teen)|eigh(ty|t|teen)|nin(ety|e|eteen)|zero|hundred|thousand|million)", "gi" ); // hundred/thousand/million etc. act as multipliers in this solution and need a seperate search var mult = new RegExp( "(hundred|thousand|million)", "gi" ); // reversing the string allows us to add largest digits first number = number.split(' ').reverse().join(" "); // while there is a number in string number // if that number is a multiplier // if that number is 100 -&gt; multiplier = multiplier * 100; // else multiplier = reference value; // else value = value + reference value * multiplier // end while value = 0; multiplier = 1; while( a = find.exec(number) ) { if( m = mult.exec(a[0]) ) { if( m[0] == 'hundred' ) { multiplier *= 100; } else { multiplier = ref[m[0]]; } } else { value += ref[a[0]] * multiplier; } } return value; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload