How to check if eval name is undefined

I have a form that is basically a calculator. you can enter an equation and it will evaluate it. I also have 2 memory fields (text fields with the name m1 and m2) where you can enter something and it will hold that value, and then when you enter an expression in the first field, you can refer to m1 or m2 in its equation, and this will be evaluated using the numbers entered in the memory fields.

The problem is that you are trying to reference m1 or m2 in your equation, and the text fields are empty, you get an undefined error.

I spun my wheels for hours to try to put some sort of check on the fact that if the equation is evaluated to undefined, just show the pop-up window. I need this in raw javascript . any help is appreciated.

function displayResult(thisElement) { thisElement.value = eval(thisElement.value); <!-- this line throws the error if you use m1 and no m1 is defined, m2 and no m2 is defined, etc --> if(!(thisElement.value>0)) { thisElement.value=0; } } function mem(v) { v.value = eval(v.value); eval(v.name+"="+v.value); } <input id="calcFormula" name="calculate" size="40" /> <input type="submit" value="Calculate" onclick="displayResult(this.form.calculate);" /> <input name="m1" id="m1" size="12" onchange="mem(this);" value="0" /> <input name="m2" id="m2" size="12" onchange="mem(this);" value="0" /> 
+6
source share
2 answers

I can present three solutions:

  • You can assume that empty m1 / m2 means 0, so it will never be undefined. It really simplifies.
  • You can use regexp to check first for any occurrence of m1 or m2 in the equation, and if it exists, then check if it is undefined.
  • But the best way is to use try ... catch .

Try / catch example:

 try { eval('12+3+m1'); } catch (e) { alert(e.message); } 
+7
source

since it should load data from form fields, but m1 is not an access key for the form field. and m1 is not a global variable, so it fails. create 2 global variables and let the forms m1 and m2 keep their values ​​there when changing.

your original script fails because your function evaluates m1 and m2, but they are destroyed when the function ends, because they are not a global scope

  function displayResult(thisElement) { thisElement.value = eval(thisElement.value); <!-- this line throws the error if you use m1 and no m1 is defined, m2 and no m2 is defined, etc --> if(!(thisElement.value>0)) { thisElement.value=0; } } m1=0; m2=0; <input id="calcFormula" name="calculate" size="40" /> <input type="submit" value="Calculate" onclick="displayResult(this.form.calc ulate);" /> <input name="m1" id="m1" onchange="m1=this.value" size="12" value="0" /> <input name="m2" id="m2" size="12" onchange="m2=this.value;" value="0" /> 

Why your source code failed:

 Step 1. M1 is stored in the form. step 2. Onchange event is fired step 3. Function mem(this) is called step 4. Entering scope of function mem step 5. Inserting this.name into string step 6. Inserting this.value into string Step 7. evalling string step 7.1 found code m1=1 step 7.1.1 check window object for variable named m1 step 7.1.1.1 failed. Create local variable for function mem called m1 step 7.1.1.2 assign value 1 to scope variable m1 step 7.1.1.3 exit eval step 7.1.1.4 exit function mem(this) step 7.1.1.5 check for scoped variables step 7.1.1.6 scoped variable found m1 step 7.1.1.7 destroy scoped variable and free up memory step 7.1.2.2 passed. retrieve pointer to window object variable m1 step 7.1.2.3 assign value 1 to window.m1 step 7.1.2.4 exit eval step 7.1.2.5 exit function mem(this) step 7.1.2.6 check for scoped variables step 7.1.2.7 none found, resume other tasks. 
+1
source

Source: https://habr.com/ru/post/919281/


All Articles