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

266
Vistas
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 Respuestas
Responde la pregunta

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