Calculator, problem trying to evaluate with eval() function


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);**_
_ _
_ }_


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;


It works!

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

1 Like