Calculator, problem trying to evaluate with eval() function

Hi,

Im trying to build the JS Calculator and I´m stuck coding the = button. The screen of the calculator goes inside this div:

*div id=“screen” input type=“text” size=“26”>

The button has the following HTML code:

**<input type=“button” class=“buttons” value= “=” onclick=“result()”>_

and theJS c ode:

function result(){
_ eval(document.getElementById(‘screen’).innerHTML);**_
_ _
_ }_

codepen: https://codepen.io/Seonatic/pen/YerdZw

I appreciate your help :blush:

what does console.log(document.getElementById(‘screen’).innerHTML) gives?

you need to set a variable to reference the eval(), like let x = eval(‘2+2’);

I tried and doesnt work anyway

function result(){
let x =document.getElementById(‘screen’).innerHTML;
eval(‘x’);

}

It works!

function result(){
let x =document.getElementById(‘screen’).innerHTML;
let res = eval(x);

1 Like