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

308
Vistas
How can I use Prompt, if and else in Javascript function to select arithmetic symbol and do some calculation with the user inputs

I am a novice in this and it is really making me to lose my hair; I can find what I'm doing wrong, please help. I am doing this in javascript. It doesn't show any error, nor display any result either. This is what I have:

    var sumIt;
    var subtractIt;
    var multiplyIt;
    var divideIt;
    var operatorOpt = prompt("Select operator");
    
    function showResult(whatResult) {
        document.write(whatResult);
        document.write("<br>");
    }
    
    var doSomething = function(num1, num2) {
        if (operatorOpt == sumIt) {
            showResult("The result is: " + (num1 + num2));
    
    
        } else if (operatorOpt == subtractIt) {
            showResult("The result is: " + (num1 - num2));
    
        }  else if (operatorOpt == multiplyIt) {
            showResult("The result is: " + (num1 * num2));
    
    
    }  else if (operatorOpt == divideIt) {
            showResult("The result is: " + (num1 / num2));
    
    doSomething(parseInt (prompt("Enter first number: ")) ,  parseInt (prompt("Enter second number: ")))

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Simply at the prompt create a list with the options like so:

    prompt("Select operator:" 
    + "\n1. Addition" 
    + "\n2.Subtraction" 
    + "\n3.Multiplication" 
    + "\n4.Division"); 

Then compare the user supplied numbers whit the operator number.

about 4 years ago · Juan Pablo Isaza Denunciar

0

It seems that your are missing a closed bracket in the definition of doSomething function. The following code seems to work produce the desired results

var sumIt = "+";
var subtractIt = "-";
var multiplyIt = "*";
var divideIt = "/";
var operatorOpt = prompt("Select operator");

function showResult(whatResult) {
    console.log(whatResult);
    document.write(whatResult);
    document.write("<br>");
}

var doSomething = function(num1, num2) {
    if (operatorOpt == sumIt) {
        showResult("The result is: " + (num1 + num2));
    } else if (operatorOpt == subtractIt) {
        showResult("The result is: " + (num1 - num2));
    }  else if (operatorOpt == multiplyIt) {
        showResult("The result is: " + (num1 * num2));
    }  else if (operatorOpt == divideIt) {
        showResult("The result is: " + (num1 / num2));
    } else {
      console.log("No Condition reached");
    }

 }

 doSomething(parseInt (prompt("Enter first number: ")) ,  parseInt (prompt("Enter second number: ")));
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