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

263
Views
I can't display the result of a calculator in javascript, the rest of the code doesn't work

Hello I am trying to create a calculator but I can not display the result. I first tried to store the return in a variable, then I stored my function in a variable I did an alert() then I did an alert with pipe for my different functions after my switch condition but nothing works.

Here’s the code I made. Can you tell me how to display the result?

let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
let firstNumber = parseInt(prompt('write your first number'));
let secondNumber = parseInt(prompt('write your second number'));
let result;

do {
  isNaN()
} while (firstNumber, secondNumber);

result = function addition() {
  return firstNumber + secondNumber;


}

result = function multiplication() {
  return firstNumber * secondNumber;

}

result = function soustraction() {
  return firstNumber - secondNumber;

}

result = function division() {
  return firstNumber / secondNumber;

}

switch (choice) {
  case "1":
    addition();
    break;
  case "2":
    multiplication();
    break;
  case "3":
    soustraction();
    break;
  case "4":
    division();
    break;

  default:
    'This number isn\'t in the list'
}
alert(result(firstNumber, secondNumber));

try {
  switch (division) {
    case secondNumber = 0:
      alert('You can\'t split by 0');

  }
} catch {
  console.error(error.stack);
}

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

I rewrote things a little bit to get it to work. Here was my approach to what you were attempting:

let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
        function checkFirstNumber(anotherTry = null) {
            let bad = true;
            let firstIn;
            if (anotherTry == null) {
                firstIn = prompt('write your first number')
            } else {
                firstIn = prompt('You did not enter a valid number. Please write your first number.')
            }
            bad = isNaN(firstIn);
            if (bad != false) {
                checkFirstNumber(true);
            } else {
                return parseFloat(firstIn);
            }
        }
        function checkSecondNumber(anotherTry = null) {
            let bad = true;
            let firstIn;
            if (anotherTry == null) {
                firstIn = prompt('write your second number')
            } else {
                firstIn = prompt('You did not enter a valid number. Please write your second number.')
            }
            bad = isNaN(firstIn);
            if (bad != false) {
                checkFirstNumber(true);
            } else {
                return parseFloat(firstIn);
            }
        }
let firstNumber = checkFirstNumber();
let secondNumber = checkSecondNumber();
let result;

// do {
//   isNaN()
// } while (firstNumber, secondNumber);

function addition(inFirst, inSecond) {
  return inFirst + inSecond;


}

function multiplication(inFirst, inSecond) {
  return inFirst * inSecond;

}

function soustraction(inFirst, inSecond) {
  return inFirst - inSecond;

}

function division(inFirst, inSecond) {
  return inFirst / inSecond;

}

switch (choice) {
  case "1":
    result = addition(firstNumber, secondNumber);
    break;
  case "2":
    result = multiplication(firstNumber, secondNumber);
    break;
  case "3":
    result = soustraction(firstNumber, secondNumber);
    break;
  case "4":
    if (secondNumber == 0) {
        result = 'You can\'t divide by 0. Please try again.';
    } else {
        result = division(firstNumber, secondNumber);
    }
    break;

  default:
    'This number isn\'t in the list'
}
alert(result);
location.reload();

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