最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论