What is the alternative of eval()?

Hi Guys!
I try to code my calculator app. But I saw that eval() can’t be used.
Do you know what is the alternative of eval()?
This is my code:

window.onload = function() {
var butt = document.getElementById(‘button’);
var result = document.getElementById(‘results’);
var clear = document.getElementById(“ac”);

document.addEventListener('click', function(event){
  if(event.target.nodeName == "BUTTON") {
    var value = event.target.innerHTML;

    if(value === '=') {
      results.innerHTML = value[results.innerHTML];
    }else {
    results.innerHTML += value;
}, false);


Thank You for your feedback.

1 Like

To manually parse and calculate the string, rather than simply running it through eval().

Suppose you’re storing the entire expression as a long string, and showing it on the display of your calculator. You might end up with something like this:


and something like that would eval nicely. However, the logic in this one requires that you split the string into parts at each operator (+ - * /) and perform that operation. Whether you simply do left-to-right calculation, or order of operations calculation, is entirely up to you.

Personally, rather than storing it in a string, I found it easier to update an operations array each time an operator, the AC or the equals was pressed. Doing this, I wasn’t having to parse the string and do the calculations. The string was all parsed for me, and I simply wrote a collapse() function for my operations array, which combined numbers based on the operator between them.

1 Like

Thank your for your feedback!
Can you give me some examples, please?

That was sort of the point of the closing paragraph. I created an array when I create the calculator, call it whatever, to be your operations stack.

Every time an operator is pressed, check the displayed string. If it contains just the operator, then replace the last operator. if it contains a number AND the operator, then push the number on the operation stack, then the operator, and clear the display (usually, display 0 so the user knows something has happened).

When the AC is hit, simply clear the entire array. Boom done.

When the = is hit, then you need to “collapse” the operation stack. How you do that depends on your order of operations. I was lazy and just went with “left-to-right” calculation. So I remove the first three members of the array (which would be a number, an operator, a number). I used logic to determine what operation it was, and combine those two numbers with that operator. Then, I put the result back on the FRONT of the operation stack, making it the new first number. Then re-loop. And keep re-looping, until the stack consists of a single member, at which point it is completely collapsed, and I display that single member as the result.

I’m not really comfortable getting much more detailed than that, as this is one of the challenges. At this point, the mechanical aspects of this should be pretty easily understood. The logic is the part that should be your biggest challenge, and I think that those last few paragraphs are a pretty solid start on that.

1 Like

Thank you but you can explain me with code. It is not because I don’t understand what you mean but it is easier for me with code.

you can use Spread operator demo