Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let myInput = "one billion one million nineteen" //take the input here
- let splitMyInput = myInput.split(" ") //get the word of my input
- let storeData = [] //a array what contain the integer value
- let finalRes = 0 //to store the final number what is convert from string
- //main function
- splitMyInput.map((element, ind) => {
- if(element){ //if the element is true the it will executed
- let myInputString = element.toLowerCase() //convert the string to lower case
- let currentNumber = findWord(myInputString) //find the number
- let myArraySecondData = splitMyInput[ind + 1] //get the second data of the splitMyInput array which is the split array
- if(myArraySecondData){ //if the second data exist it will execute
- let secondData = myArraySecondData.toLowerCase() //convert the second data to lower case
- 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
- storeData.push(currentNumber)
- secondData = findWord(secondData) //get the second data in integer format
- let getTheNumber = findWord(secondData)
- let calculation = secondData * currentNumber
- finalRes += calculation //store the sum in to tha mail final result
- getTheValue(storeData,getTheNumber)
- }
- }else if(myInputString == "hundred" || myInputString == "thousand" || myInputString == "million" || myInputString == "billion" || myInputString == "lac"){
- return 0 //if the current tacking string is these then it will return 0
- }else{
- finalRes += currentNumber //store the sum in to final res variable.
- }
- }
- })
- //if the value is hundred, thousand, million,billion, lac then it will executed
- function getTheValue (arr, str){
- let [myInputString, storeData] = [str, arr] //store the argument variable
- myInputString == "hundred" && (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
- myInputString == "thousand" && (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
- myInputString == "million" && (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
- myInputString == "billion" && (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
- myInputString == "lac" && (finalRes += storeData[storeData.length - 2] * storeData[storeData.length - 1])
- }
- //convert the string in to a data here
- function findWord (string){
- let [str, res] = [string]
- switch(str){
- case "one": {
- res = 1;
- break
- }
- case "two": {
- res = 2;
- break
- }
- case "three": {
- res = 3;
- break
- }
- case "four": {
- res = 4;
- break
- }
- case "five": {
- res = 5;
- break
- }
- case "six": {
- res = 6;
- break
- }
- case "seven": {
- res = 7;
- break
- }
- case "eight": {
- res = 8;
- break
- }
- case "nine": {
- res = 9;
- break
- }
- case "ten": {
- res = 10;
- break
- }
- case "eleven": {
- res = 11;
- break
- }
- case "twelve": {
- res = 12;
- break
- }
- case "thirteen": {
- res = 13;
- break
- }
- case "fourteen": {
- res = 14;
- break
- }
- case "fifteen": {
- res = 15;
- break
- }
- case "sixteen": {
- res = 16;
- break
- }
- case "seventeen": {
- res = 17;
- break
- }
- case "eighteen": {
- res = 18;
- break
- }
- case "nineteen": {
- res = 19;
- break
- }
- case "twenty": {
- res = 20;
- break
- }
- case "thirty": {
- res = 30;
- break
- }
- case "forty": {
- res = 40;
- break
- }
- case "fifty": {
- res = 50;
- break
- }
- case "sixty": {
- res = 60;
- break
- }
- case "seventy": {
- res = 70;
- break
- }
- case "eighty": {
- res = 80;
- break
- }
- case "ninety": {
- res = 90;
- break
- }
- case "hundred": {
- res = 100
- break
- }
- case "thousand": {
- res = 1000
- break
- }
- case "million": {
- res = 1000000
- break
- }
- case "billion": {
- res = 1000000000
- break
- }
- case "lac": {
- res = 100000
- break
- }
- }
- return res
- } //find the word function
- console.log(finalRes); //print the main conversion here
Add Comment
Please, Sign In to add comment