Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Can't make the input on my calculator evaluate the sum

I have just started learning to code and am currently building my first calculator using javascript. It works coming down to the input being projected on the screen from clicking the button and the 'C' clear button clears the input screen, however I keep getting a syntax error when using the '=" button when trying to evaluate and need assistance, have tried multiple ways but am struggling when trying to use the screen.value.

// Selecting elements

const buttons = document.querySelectorAll(".btn");
let screen = document.getElementById("input");

//function to add input onto screen
const addTo = function(e) {
  screen.value += e;
};

for (let i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", function() {
    // log click to a variable
    const input = buttons[i].value;

    // add clicked input to screen
    addTo(input);

    // make screeninput evaluate sum
    if (input === "=") {
      let equals = eval(screen.value);
      equals = screen.value;
    } else if (input === "C") {
      screen.value = " ";
    }
  });
}
<h1>MY CALCULATOR</h1>
<div id=c alc class="calc-container">
  <div id=s creen class="input-container">
    <input type=text name=d isplay id=i nput class="user-input" value="" />
  </div>

  <div id="wrapper" class="button-container">

    <input value="9" type=b utton class="btn" />
    <input value="8" type=b utton class="btn" />
    <input value="7" type=b utton class="btn" />
    <input value="6" type=b utton class="btn" />
    <input value="5" type=b utton class="btn" />
    <input value="4" type=b utton class="btn" />
    <input value="3" type=b utton class="btn" />
    <input value="2" type=b utton class="btn" />
    <input value="1" type=b utton class="btn" />
    <input value="*" type=b utton class="btn" />
    <input value="0" type=b utton class="btn" />
    <input value="+" type=b utton class="btn" />
    <input value="/" type=b utton class="btn" />
    <input value="-" type=b utton class="btn" />
    <input value="=" type=b utton class="btn equals">
    <input value="." type=b utton class="btn" />
    <input value="C" type=b utton class="btn" />

  </div>
</div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In case of = you are trying to evaluate an expression with =, for example 10+2=. Which is an invalid expression. You should avoid that = from the eval statement and execute only 10+2.

Call addTo only if the input is not = and C to fix this.

// Selecting elements

const buttons = document.querySelectorAll(".btn");
let screen = document.getElementById("input");

//function to add input onto screen
const addTo = function (e) {
  screen.value += e;
};

for (let i = 0; i < buttons.length; i++) {
  buttons[i].addEventListener("click", function () {
    // log click to a variable
    const input = buttons[i].value;


    // make screeninput evaluate sum
    if (input === "=") {
      let equals = eval(screen.value);
      screen.value = equals;
    } else if (input === "C") {
      screen.value = " ";
    } else {
      // add clicked input to screen
      addTo(input);
    }
  });
}
<h1>MY CALCULATOR</h1>
<div id=calc class="calc-container">
  <div id=screen class="input-container">
    <input type=text name=display id=input class="user-input" value="">
    </input>
  </div>

  <div id="wrapper" class="button-container">

    <input value="9" type=button class="btn"></input>
    <input value="8" type=button class="btn"></input>
    <input value="7" type=button class="btn"></input>
    <input value="6" type=button class="btn"></input>
    <input value="5" type=button class="btn"></input>
    <input value="4" type=button class="btn"></input>
    <input value="3" type=button class="btn"></input>
    <input value="2" type=button class="btn"></input>
    <input value="1" type=button class="btn"></input>
    <input value="*" type=button class="btn"></input>
    <input value="0" type=button class="btn"></input>
    <input value="+" type=button class="btn"></input>
    <input value="/" type=button class="btn"></input>
    <input value="-" type=button class="btn"></input>
    <input value="=" type=button class="btn equals"></input>
    <input value="." type=button class="btn"></input>
    <input value="C" type=button class="btn"></input>

  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Because of you do eval(screen.value).

When you call the eval function, your screen.value is 'xxxx='.

This is not in the syntax of eval.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda