Calculator, problem trying to evaluate with eval() function

Calculator, problem trying to evaluate with eval() function
0.0 0

#1

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:


#2

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’);


#3

I tried and doesnt work anyway

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

}


#4

It works!

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