Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
JS calculator not working. Operators are not functioning. Every time I run the calc, it only adds

I am very new to JavaScript. I have been trying to figure this out but nothing seems to work. operators seem to be not stable. for example if i subtract 6-6, it'd still give me 12, i.e operators only add in all situations.

inp1 = prompt("Enter in a number");
inp1 = Number.parseInt(inp1);
let js1 = inp1;


op = prompt("Enter an operator");
op = ["+", "-", "*", "/"];

inp2 = prompt("Enter another number");
inp2 = Number.parseInt(inp2);
let js2 = inp2;

function addition(x, y) {
    return (x + y);
}
function subtraction(x, y) {
    return (x - y);
}
function multiplication(x, y) {
    return (x * y);
}
function division(x, y) {
    return (x / y);
}

if (op = "+") {
    console.log(addition(js1, js1));

}
else if (op = "-") {
    console.log(subtraction(js1, js2));
}
else if (op = "*") {
    console.log(multiplication(js1, js2));
}
else if (op = "/") {
    console.log(division(js1, js2));
}
else {
    console.log("Sorry! An Error has occurred");
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are not comparing them in the if statement, instead you are assigning. What you're looking for is this:

if(op === "+") {
   //add
} else if(op === "-") {
   //subtract
} 

You should also look into JavaScripts different ways of comparing https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

about 4 years ago · Juan Pablo Isaza Denunciar

0

fist problem in

op = ["+", "-", "*", "/"];

so after your get the operator from a user, you assign it to a new value

and also

if (op = "+") {}
else if (op = "-"){}

this assignment operator not the comparator instead use

if (op === "+")
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda