eval function does not work correctly in JavaScript -
i new in javascript & creating simple calculator.
but have problems eval() function.
my script:
function calc(fld){ var firstno = 0 ; var secno = 0 ; var num = fld.name.charat(2); var op = fld.name.charat(0); if(op == "t"){op = "-";} else if(op == "z"){op = "*";} else if(op == "e"){op = "=";} else if(op == "j"){op = "+";} else if(op == "d"){op = "/";} else { op ="";} if (op != "=") {eval("document.calc1.res").value += num + op ;} else { // line doesn't work correctly document.calc1.res.value = eval("document.calc1.res.value") ; // nor 1 // document.calc1.res.value = eval("document.calc1.res").value ; } and html:
<form id="calc" name="calc1" method="post"> <input type="text" name="res" id="res"><br /> <input type="button" value="7" id="no7" name="no7" onclick="calc(this)"></input type="button" value=""><input type="button" value="8" id="no8" name="no8" onclick="calc(this)"></input type="button" value=""><input type="button" value="9" id="no9" name="no9" onclick="calc(this)"></input type="button" value=""><br /> <input type="button" value="4" id="no4" name="no4" onclick="calc(this)"></input type="button" value=""><input type="button" value="5" id="no5" name="no5" onclick="calc(this)"></input type="button" value=""><input type="button" value="6" id="no6" name="no6" onclick="calc(this)"></input type="button" value=""><br /> <input type="button" value="1" id="no1" name="no1" onclick="calc(this)"></input type="button" value=""><input type="button" value="2" id="no2" name="no2" onclick="calc(this)"></input type="button" value=""><input type="button" value="3" id="no3" name="no3" onclick="calc(this)"></input type="button" value=""><br /> <input type="button" value="-" id="no1" name="t" onclick="calc(this)"> <input type="button" value="*" id="no1" name="z" onclick="calc(this)"> <input type="button" value="=" id="no1" name="e" onclick="calc(this)"> <!--this line works correctly--> <input type="button" name="doit" value=" = " onclick="document.calc1.res.value = eval(document.calc1.res.value)"> <input type="button" value="/" id="no1" name="d" onclick="calc(this)"> <input type="button" value="+" id="no1" name="j" onclick="calc(this)"> </form> in html code there 2 equal signs. problem there.
when wanted evaluate look in js file did not work, working in html file. mentioned lines comments.
what differences between these lines?
1.) don't utilize eval()! don't need to.
2.) of forms in document, [document].forms. set .calc1 specific form name set .res specific field name.
function calc(num1elem, opelem, num2elem){ var num1 = parseint(num1elem.value, 10); if (num2elem) var num2 = parseint(num2elem.value, 10); var op = opelem.value; var num; if(op == "-") num = num1-num2; else if(op == "*") num = num1*num2; else if(op == "=") num = num1; else if(op == "+") num = num1+num2; else if(op == "/") num = num1/num2; else op =""; if (op != "") { document.forms.calc1.res.value = num; //do not utilize eval(): // document.forms.calc1.res.value = eval(num1+op+num2); // won't work if utilize = sign. } //the else loop sets equal is, it's not doing anything, can omit it. } //type in 1 of these lines of code , sees happens now! calc(document.forms.calc1.no8, document.forms.calc1.e, document.forms.calc1.no7) calc(document.forms.calc1.no8, document.forms.calc1.j, document.forms.calc1.no7) calc(document.forms.calc1.no8, document.forms.calc1.t, document.forms.calc1.no7) calc(document.forms.calc1.no8, document.forms.calc1.z, document.forms.calc1.no7) calc(document.forms.calc1.no8, document.forms.calc1.d, document.forms.calc1.no7) javascript eval
No comments:
Post a Comment