Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Trying to make a calculator with javascript but doesn't give any results

I'm practicing if statements and I'm trying to make a calculator. I can enter the numbers but clicking on the add, subtract, multiply or divide buttons does not give me any results. I know there are easier ways to do it but it was just for practice. What have I done wrong?

function operar(boton) {

  var num1, num2, resultado;

  num1 = document.getElementById("num1");
  num2 = document.getElementById("num2");
  resultado = document.getElementById("resultado");

  if (boton.value == '+') {
    resultado.value = Number(num1.value) + Number(num2.value);
  } else {
    if (boton.value == '-') {
      resultado.value = Number(num1.value) - Number(num2.value);
    } else {
      if (boton.value == '*') {
        resultado.value = Number(num1.value) * Number(num2.value);
      } else {
        if (boton.value == '/') {
          resultado.value = Number(num1.value) / Number(num2.value);

        }
      }
    }
  }
}
<form>
  <div>
    <label>Número 1</label>
    <input id="num1" type="text" />
  </div>
  <div>
    <label>Número 2</label>
    <input id="num2" type="text" />
  </div>

  <input type="button" value="+" onclick="operar(this)" />
  <input type="button" value="-" onclick="operar(this)" />
  <input type="button" value="*" onclick="operar(this)" />
  <input type="button" value="/" onclick="operar(this)" />

  <div>
    <label>Resultado</label>
    <input type="text" id="resultado" />
  </div>

</form>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The snippet you gave here, this code is running perfectly. What else do you want to do?

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!