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

I am working on a simple calculator using HTML, CSS, and JavaScript. The calculator displays numbers an

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!

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!

Share Improve this question edited Jan 31 at 10:47 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Jan 31 at 10:20 Aleena JaneAleena Jane 11 bronze badge 2
  • 1 The error message suggests that evall is not recognized. So, whats the question? Define a function evall. – tkausl Commented Jan 31 at 10:22
  • define the function evall – Hmmmmm Commented Jan 31 at 10:23
Add a comment  | 

1 Answer 1

Reset to default 1

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745269186a4619640.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信