Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript parse string to integer
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThat's an interesting problem you're working on, and I can't say that I fully understand your code. There are a few gotchas in there, though, that aren't directly related to your problem. 1) you may want to avoid a name collision with the built-in 'parseInt' function. It may not be impacting your codes behavior but that's probably a bad practice to use a built-in name like that.
      singulars
    2. CO2) your `find` regular expression doesn't properly guard against matching proper prefixes - in your example JS fiddle it appears to match 'seven' as a proper prefix when it should match 'seventeen'. You'll want to include white space guards within the regular expression string to force full-word matching.
      singulars
    3. CONeat idea, I'm going to incorporate this in my French lexical analyzer to suggest number (int) representation in place of word (string) representation when numbers grow too large. Agreed with @MikeEdwards though - there are some cases that you may want to be aware of: `7` would be the result of `seven` and `seventeen` because the RegEx stops matching when it finds the first valid piece (`seven`). `4` in `four` and `fourteen` is also another example. You need a workaround, like the one he suggested. Also, isn't it interesting that every number in English can be broken down into 30 or so parts?
      singulars
 

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