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

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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