shopnilSS

Convert a string in a number

May 23rd, 2021 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let myInput = "one billion one million nineteen" //take the input here
  2. let splitMyInput = myInput.split(" ") //get the word of my input
  3. let storeData = [] //a array what contain the integer value
  4. let finalRes = 0 //to store the final number what is convert from string
  5.  
  6. //main function
  7. splitMyInput.map((element, ind) => {
  8.     if(element){ //if the element is true the it will executed
  9.         let myInputString = element.toLowerCase() //convert the string to lower case
  10.         let currentNumber = findWord(myInputString) //find the number
  11.         let myArraySecondData = splitMyInput[ind + 1] //get the second data of the splitMyInput array which is the split array
  12.         if(myArraySecondData){ //if the second data exist it will execute
  13.             let secondData = myArraySecondData.toLowerCase() //convert the second data to lower case
  14.             if(secondData == "hundred" || secondData == "thousand" || secondData == "million" || secondData == "billion" || secondData == "lac") { //check that is there have any hundred.... etc data as a second data ref to the current tacking data
  15.                 storeData.push(currentNumber)
  16.                 secondData = findWord(secondData) //get the second data in integer format
  17.                 let getTheNumber = findWord(secondData)
  18.                 let calculation = secondData * currentNumber
  19.                 finalRes += calculation //store the sum in to tha mail final result
  20.                 getTheValue(storeData,getTheNumber)
  21.             }
  22.         }else if(myInputString == "hundred" || myInputString == "thousand" || myInputString == "million" || myInputString == "billion" || myInputString == "lac"){
  23.             return 0 //if the current tacking string is these then it will return 0
  24.         }else{
  25.             finalRes += currentNumber //store the sum in to final res variable.
  26.             }
  27.         }
  28. })
  29.  
  30. //if the value is hundred, thousand, million,billion, lac then it will executed
  31. function getTheValue (arr, str){
  32.     let [myInputString, storeData] = [str, arr] //store the argument variable
  33.     myInputString == "hundred" &&   (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
  34.     myInputString == "thousand" &&  (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
  35.     myInputString == "million" &&   (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
  36.     myInputString == "billion" &&   (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
  37.     myInputString == "lac" &&   (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
  38. }
  39.  
  40.  
  41. //convert  the string in to a data here
  42. function findWord (string){
  43.     let [str, res] = [string]
  44.     switch(str){
  45.         case "one": {
  46.            res = 1;
  47.            break
  48.         }
  49.         case "two": {
  50.            res = 2;
  51.            break
  52.         }
  53.         case "three": {
  54.            res = 3;
  55.            break
  56.         }
  57.         case "four": {
  58.            res = 4;
  59.            break
  60.         }
  61.         case "five": {
  62.            res = 5;
  63.            break
  64.         }
  65.         case "six": {
  66.            res = 6;
  67.            break
  68.         }
  69.         case "seven": {
  70.            res = 7;
  71.            break
  72.         }
  73.         case "eight": {
  74.            res = 8;
  75.            break
  76.         }
  77.         case "nine": {
  78.            res = 9;
  79.            break
  80.         }
  81.         case "ten": {
  82.            res = 10;
  83.            break
  84.         }
  85.         case "eleven": {
  86.            res = 11;
  87.            break
  88.         }
  89.         case "twelve": {
  90.            res = 12;
  91.            break
  92.         }
  93.         case "thirteen": {
  94.            res = 13;
  95.            break
  96.         }
  97.         case "fourteen": {
  98.            res = 14;
  99.            break
  100.         }
  101.         case "fifteen": {
  102.            res = 15;
  103.            break
  104.         }
  105.         case "sixteen": {
  106.            res = 16;
  107.            break
  108.         }
  109.         case "seventeen": {
  110.            res = 17;
  111.            break
  112.         }
  113.         case "eighteen": {
  114.            res = 18;
  115.            break
  116.         }
  117.         case "nineteen": {
  118.            res = 19;
  119.            break
  120.         }
  121.         case "twenty": {
  122.            res = 20;
  123.            break
  124.         }
  125.         case "thirty": {
  126.            res = 30;
  127.            break
  128.         }
  129.         case "forty": {
  130.            res = 40;
  131.            break
  132.         }
  133.         case "fifty": {
  134.            res = 50;
  135.            break
  136.         }
  137.         case "sixty": {
  138.            res = 60;
  139.            break
  140.         }
  141.         case "seventy": {
  142.            res = 70;
  143.            break
  144.         }
  145.         case "eighty": {
  146.            res = 80;
  147.            break
  148.         }
  149.         case "ninety": {
  150.            res = 90;
  151.            break
  152.         }
  153.         case "hundred": {
  154.            res = 100
  155.            break
  156.         }
  157.         case "thousand": {
  158.            res = 1000
  159.            break
  160.         }
  161.         case "million": {
  162.            res = 1000000
  163.            break
  164.         }
  165.         case "billion": {
  166.            res = 1000000000
  167.            break
  168.         }
  169.         case "lac": {
  170.            res = 100000
  171.            break
  172.         }
  173.     }
  174.     return res
  175. } //find the word function
  176.  
  177.  
  178. console.log(finalRes); //print the main conversion here
Add Comment
Please, Sign In to add comment