CodeWars: ParseInt() Reloaded

In this example below you will see how to do a CodeWars: ParseInt() Reloaded with some HTML / CSS and Javascript

Convert string to numbers, from zero through one million."one" => 1 "twenty" => 20 "two hundred forty-six" => 246

Thumbnail
This awesome code was written by elaboratecon, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright elaboratecon ©
  • HTML
  • CSS
  • JavaScript
    Open the console...

/*Downloaded from https://www.codeseek.co/elaboratecon/codewars-parseint-reloaded-JLQQXX */
    


/*Downloaded from https://www.codeseek.co/elaboratecon/codewars-parseint-reloaded-JLQQXX */
    const parseNum = str => {
  const numLib = {
    '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',
    'eightteen': '18',
    'nineteen': '19',
    'twenty': '20',
    'thirty': '30',
    'forty': '40',
    'fifty': '50',
    'sixty': '60',
    'seventy': '70',
    'eighty': '80',
    'ninety': '90',
    'hundred': '00',
    'thousand': '000',
    'million': '0000',
  }
  //str = str.split('-').join(' ').split(' and ').join(' ').split(' ')
  str = str.split(' ')
  str = Number(str.map((num) => numLib[num]).join(''))
  
  return str
}



//console.log(parseNum("one")) // 1
//console.log(parseNum("twenty"))  // 20
console.log(parseNum("two hundred and forty-six")) // 246
//console.log(parseNum("seven hundred eighty-three thousand nine hundred and nineteen")) // 783919



Comments