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

144
Views
How to create a simple calculator using switch case and function using Javascript

I'm new to Javascript and just finished learning switches and conditionals and I wanted to create my calculator where it first asks the user what they want to do and gets the two digits from them and goes ahead and alerts them with an answer. But every time I try to run the function it gives me an undefined error enter code here

let tell = prompt("What would you  like to do (*,/,+,-)");
let dig1 = prompt("Enter first Number");
let dig2 = prompt("Enter second Number")

function calculate(a,b) {
    let answer = tell;
    switch (a,b) {
        case "*":
            answer = a * b;
            break;
        case "/":
            answer = a / b;
            break;
        case "+":
            answer = a + b;
            break;
        case "-":
            answer = a - b;
            break
    }
}

alert(calculate(dig1,dig2))
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

let tell = prompt("What would you  like to do (*,/,+,-)");
let dig1 = prompt("Enter first Number");
let dig2 = prompt("Enter second Number")

function calculate(a,b) {
    // check the value of the `tell` is one of these cases and return the result.
    switch (tell) {
        case "*":
            return a * b;
        case "/":
            return a / b;
        case "+":
            return a + b;
        case "-":
            return a - b;
        default:
            return "You didn't a correct opertor"
    }
}

alert(calculate(+dig1, +dig2))

about 4 years ago · Santiago Trujillo 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!