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

282
Visualizações
Calling a function in an if-else statement

I'm trying to get the add() function to calculate in the if-else statement and not the line commented out for part of an assignment but I'm not sure how to get it to show the result in a new window.

function add(){
   return +num1 + +num2
};

const num1 = window.prompt("Enter a number: ");
const operatorInput = window.prompt("Do you want to add, subtract, multiply, or divide?");
let num2;

if (operatorInput === "add" || operatorInput === "Add") {
    num2 = window.prompt("Second Number: ");
    // add()
    // let sum= parseInt(num1) + parseInt(num2)
    // alert(num1 + " " + "+" + " " + num2 + " " + "=" + " " + add());
    alert(`${num1} + ${num2} = ${add()}`);
};

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

0

Not sure if that's what you need.

var num1 = parseFloat(window.prompt("Enter a number: "));
var operatorInput = window.prompt(
    "Do you want to add, subtract, multiply, or divide?"
);
while (
    operatorInput.toLowerCase() != "add" &&
    operatorInput.toLowerCase() != "multiply" &&
    operatorInput.toLowerCase() != "divide" &&
    operatorInput.toLowerCase() != "subtract"
) {
    alert("Invalid Input! Please try again");
    operatorInput = window.prompt(
        "Do you want to add, subtract, multiply, or divide?"
    );
}
var num2 = parseFloat(window.prompt("Second Number: "));
var resultWindow = window.open("", "Result Window", "width=200,height=200");
if (operatorInput.toLowerCase() == "add")
    resultWindow.document.write(
        `<p>${num1 + " " + "+" + " " + num2 + " " + "=" + " " + (num1 + num2)
        }</p>`
    );
else if (operatorInput.toLowerCase() == "multiply")
    resultWindow.document.write(
        `<p>${num1 + " " + "*" + " " + num2 + " " + "=" + " " + num1 * num2
        }</p>`
    );
else if (operatorInput.toLowerCase() == "divide")
    resultWindow.document.write(
        `<p>${num1 + " " + "/" + " " + num2 + " " + "=" + " " + num1 / num2
        }</p>`
    );
else
    resultWindow.document.write(
        `<p>${num1 + " " + "-" + " " + num2 + " " + "=" + " " + (num1 - num2)
        }</p>`
    );

You should be careful with using new windows to display information, it's not recommended and many browsers now adays automatically blocks these actions and consider them as spam popup windows if they're directly invoked by javascript. If the results are not being shown for you make sure to allow your browser to allow popups from the link you're displaying it from.

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