0

I would like to turn this:

let nums = [1, 2, 3];
let syms = ["+", "*"];
let result;

Into a math expression:

result = 1 + 2 * 3;
console.log(result); // 7

Without using eval(), because it returns weird stuff like i.e. "6.3 + 0.6" = 6.8999999999999995

  • 1
    `6.3 + 0.6` is going to return “weird” stuff no matter whether you use eval, or not. `result = 6.3 + 0.6; console.log(result)` gets you the exact same `6.899…` here. The explanation for this behavior you can find here, https://stackoverflow.com/questions/588004/is-floating-point-math-broken – CBroe Jun 02 '20 at 13:28
  • 2
    But 6.3 + 0.6 = 6.89999 is just from floating-point inaccuracy. If you put `6.3 + 0.6` into chrome console you will get 6.89999. Thats just JS. – Jacob Jun 02 '20 at 13:28
  • Have a look at [bigNumbers.js](https://mathjs.org/docs/datatypes/bignumbers.html) that solves round-off errors – Jeremy Thille Jun 02 '20 at 13:39
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Dave Cooper Jun 02 '20 at 14:36
  • No, because my question wasn't specifically about the floating point problem, but good to know nonetheless. – Aleksandar Ivanov Jun 03 '20 at 03:32

1 Answers1

1

Most bug free answer so far......

var nums=[1,0],syms=['/']
const rulesyms = ['/','*'];  //do not change sequence of this one.
var res, count=0,power=0;

solve_nonsumoperator().then(()=>{
    summing().then(()=>{
        console.log(nums[nums.length-1])
    })
})

function getnums(j,operator){
    power=0
    if(Math.floor(nums[j])!=nums[j])
        power=nums[j].toString().split('.')[1].length
    else{         //if not a float then forcing float point
        nums[j]=nums[j].toFixed(1)
        power=1
    }
    if(Math.floor(nums[j+1])!=nums[j+1]){
        if(power<nums[j+1].toString().split('.')[1].length){
            power=nums[j+1].toString().split('.')[1].length
        }
    }
    else{
        nums[j+1]=nums[j+1].toFixed(1)
        if(power<1)
            power=1
    }
    //adding decimal 0s to match floating point 
    for(let i = nums[j].toString().split('.')[1].length;i<power;i++)
        nums[j]=nums[j].toString()+'0'
    for(let i = nums[j+1].toString().split('.')[1].length;i<power;i++)
        nums[j+1]=nums[j+1].toString()+'0'
    nums[j]=parseInt(nums[j].toString().split('.').join(''),10)
    nums[j+1]=parseInt(nums[j+1].toString().split('.').join(''),10)
    switch(operator){
        case '/':res=nums[j]/nums[j+1]
                 break;
        case '*':res=nums[j]*nums[j+1]/Math.pow(10,2*power)
                 break;
        default:if(operator==='+')
                    res=(nums[j]+nums[j+1])/Math.pow(10,power)
                else if(operator==='-')
                    res=(nums[j]-nums[j+1])/Math.pow(10,power)
                 break;
    }
}

function redundant(j){
    nums[j+1]=res //replacing the result into array position
    for(var k = j;k>0;k--){
    //loop to replace the used nums, syms to begin of array
        nums[k]=nums[k-1]
        syms[k]=syms[k-1]  
    }
    syms[count]='$' //has to be below for loop
    count++
}

function summing(){
    syms.forEach((g,j)=>{
        if(g==='+'||g==='-'){
            getnums(j,g)
            redundant(j)                  
        }    
    })
    return Promise.resolve()
}

function solve_nonsumoperator(){
    rulesyms.forEach(f=>{
        syms.forEach((g,j)=>{
            if(g===f){
                getnums(j,f)
                redundant(j)
            }    
        })
    })
    return Promise.resolve()
}