samedi 21 avril 2018

check eval() error in Javascript

I made a calculator using html css Javascript , i came through so many input problems , so to calculate the result ( '=' ) i used the function eval() , i want to now what does it return exactly and how to spot errors and fix them, for example : typing ***/+6 and clicking on the '=' wont do nothing , i want to display an error message , i tried try-catch but it didn't work :

function res() { 
    var r=eval(document.getElementById("_input").textContent); 
    try{ 
        eval(document.getElementById("_input").textContent);        
    }   
    catch(error){   
        document.getElementById("res").innerHTML="Error"; 
    }
}

Aucun commentaire:

Enregistrer un commentaire