-2

I have a calculator in html that works with javascript User input its values in one single input field, all in decimal. But for mistake sometimes user types 010 instead 10 and eval gives octa result

Sample 1: eval('20*15+8*10'); Result 380 (Correct)

Sample 2: eval('20*15+8*010'); Result 364 (Wrong)

Is there any other method I can use? or How can I cast all values to decimal before using eval?

Here my code:

function recalcula()
{
    var doc=document.forma;
     try
    {

    s_1=0;
    for(i=1;i<=9;i++)
    {
        CorrigeCampo(eval('doc.a1_'+i),0);
        abc=eval('doc.a1_'+i+'.value.replace(/,/g,"")');
        x_1=parseFloat(eval(abc));


        //x_1=parseFloat(eval(abc.replace(/\b0(\d+)\b/g, '$1')));

        if((isNaN(x_1))||(x_1==0)) 
        {
            x_1=0;
            eval('doc.a20_'+i+'.value="";');
            eval('doc.a1_'+i+'.value="";');
        }
        else
            eval('doc.a20_'+i+'.value=x_1.toFixed(4);');


        s_1+=x_1;
    }

    doc.Suma_1.value=s_1.toFixed(4);

}
catch(e)
{}

}

function CorrigeCampo(campo,def)
{

    cadenanueva="";
    valorcampo=campo.value;

    carpermitidos="0123456789.*/-+";
    for(j=0;j<valorcampo.length;j++)
    {
        act=valorcampo.substring(j,j+1);
        if(carpermitidos.indexOf(act)>=0)
        {

            cadenanueva+=act;

        }
    }
    if(cadenanueva=="")
        cadenanueva=def;
    campo.value=cadenanueva;
}
Gerardo Abdo
  • 1,150
  • 3
  • 10
  • 16
  • 1
    https://stackoverflow.com/questions/850341/how-do-i-work-around-javascripts-parseint-octal-behavior – mplungjan Dec 13 '17 at 16:12
  • It's not duplicate, did you read the answer and the question ? – Titouan56 Dec 13 '17 at 16:14
  • I cannot use parseFloat or parseInt for expressions, only for numbers. Your URL is to parse only numbers – Gerardo Abdo Dec 13 '17 at 16:15
  • Should have been duplicate of @mplungjan 's comment. Used the wrong SO post. – Nope Dec 13 '17 at 16:15
  • 1
    Please click the `<>` and create a [mcve] - you can extract the numbers with \d and parseFloat them – mplungjan Dec 13 '17 at 16:17
  • 1
    In general @GerardoAbdo don't use `eval` , specially not with user specified data. There should be no need to use `eval` - ` I cannot use parseFloat or parseInt for expressions` , you were to use it on the "number" before using it in your expression. – Nope Dec 13 '17 at 16:18
  • Still not a duplicate with this other link. Solution you gave (not using eval + extract with \d and parseFloat) are in my answer that has been downvoted) What the point of duplicate my answer in commment ? – Titouan56 Dec 13 '17 at 16:20
  • `var s = '20*15+8*010'; s = s.replace(/(\d+)/g, function(digits) { return parseInt(digits,10) }) console.log(eval(s));` https://jsfiddle.net/mplungjan/7d9kbfqz/ – mplungjan Dec 13 '17 at 16:26

1 Answers1

0

You can:

  • Use regex to identify numbers and remove leading 0
  • Use regex to identify number and surround number with parseInt(number, 10);
  • Don't use eval and make a real lexer parser
Titouan56
  • 6,932
  • 11
  • 37
  • 61