JavaScript Calculator

I know this is meant for C++ but i think there must be someone who could help me with js :)

i have a code for a simple calculator in js but there's a problem, i need to replace the "Solve" function to something else that doesn't have "eval" in it
i just need something really simple :\


<!doctype html>
<html>
<head>
<script>

function Cal(v){
document.getElementById("a1").value+=v;
}
function solve(){
var v2= document.getElementById("a1").value;
let res = eval(v2);
document.getElementById("a1").value=res;
}
function reset(){
document.getElementById("a1").value = '';
}
</script>

</head>
<body>

<table border="1">
<tr>
<td colspan="3"><input type="text" id="a1"></td>
<td><input type="button" value="+" onClick="Cal('+')"></td>
</tr>
<tr>
<td><input type="button" value="1" onClick="Cal(1)" ></td>
<td><input type="button" value="2" onClick="Cal(2)"></td>
<td><input type="button" value="3" onClick="Cal(3)"></td>
<td><input type="button" value="-" onClick="Cal('-')"></td>
</tr>
<tr>
<td><input type="button" value="4" onClick="Cal(4)"></td>
<td><input type="button" value="5" onClick="Cal(5)"></td>
<td><input type="button" value="6" onClick="Cal(6)"></td>
<td><input type="button" value="*" onClick="Cal('*')"></td>
</tr>
<tr>&nbsp;
<td><input type="button" value="7" onClick="Cal('7')"></td>
<td><input type="button" value="8" onClick="Cal('8')"></td>
<td><input type="button" value="9" onClick="Cal('9')"></td>
<td><input type="button" value="/" onClick="Cal('/')"></td>
</tr>
<tr>
<td><input type="button" value="0" onClick="Cal('0')"></td>
<td>&nbsp;</td>
<td><input type="button" value="Reset" onClick="reset()"></td>
<td><input type="button" value="=" onClick="solve()"></td>
</tr>
</table>
</body>
</html>
I don't know JavaScript very well, but I assume the 'v2' variable will be some sort of string?

Can you show examples of what this string could be? Depending on the complexity of this string, parsing and computing the output might be easy or a challenge. You'd have to use something like the Shunting Yard algorithm.
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Last edited on
the googler sez:
Fortunately, there's a very good alternative to eval(): using window.Function(). It appears to be rather simple to one-for-one replace with this.

eval is 'bad' -- its like our system() function, you can feed it evil things to make it a vulnerability. It apparently is not only a liability, it is also rather slow.

by the way, this is a forum for C and C++. You may get better help if you asked js experts instead... like Ganado, I only dabble with js... If you need it I can give you my (C++) reverse polish calculator, but its not an expression parser, GIGO and all that.
Last edited on
It looks like the only possible to use addition, subtraction, multiplication, and division. There aren't any parentheses to deal with either.

Its not ideal - but you could loop through the string twice. Once to do all the division and multiplication that's present in the string, then another time to do the addition and subtraction.
Topic archived. No new replies allowed.