Aleena Jane
Aleena Jane

Reputation: 1

An error in JS code about a Calculator that show me "ERROR" result

I am working on a simple calculator using HTML, CSS, and JavaScript. The calculator displays numbers and operators correctly, but when I press the "=" button to evaluate the expression, I get an error in the console:

Uncaught ReferenceError: evall is not defined at calculate (index.html:49) at HTMLButtonElement.onclick (index.html:37) Code: HTML:

<input type="text" class="display" id="display" disabled>
<button class="equal" onclick="calculate()">=</button>

JavaScript Code:

function calculate() {
    try {
        document.getElementById("display").value = evall(document.getElementById("display").value); // Possible issue here
    } catch (error) {
        document.getElementById("display").value = "Error";
    }
}

The error message suggests that evall is not recognized.

This post provides enough context and details for Stack Overflow users to understand your issue and offer solutions. Let me know if you need any modifications!

Upvotes: 0

Views: 32

Answers (1)

Nathan Jones
Nathan Jones

Reputation: 34

I don't know if you have figured this out yet but I think your issue is that evall isn't a JS function, but eval is. The quick code fix is this:

document.getElementById("display").value = eval(document.getElementById("display").value);

However, .eval does have some security issues, and also make sure to remove the disabled tag in the input.

Hope this helps!

Upvotes: 1

Related Questions