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

306
Views
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 answers
Answer question

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 Report

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