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

236
Vistas
¿Cómo imprimo el valor de retorno en la consola?

Tengo una tarea del curso para hacer una calculadora en Javascript. Tengo las funciones para los diferentes operadores y el cambio de caso, pero tengo problemas para imprimir los valores de retorno de las funciones. Aquí está el código que tengo hasta ahora:

 let firstValue = prompt("Enter a number"); let secondValue = prompt("Enter a second number"); let operation = prompt("How should these numbers interact"); function addition(firstValue, secondValue) { return firstValue + secondValue; } function subtraction(firstValue, secondValue) { return firstValue - secondValue; } function division(firstValue, secondValue) { return firstValue / secondValue; } function multiplication(firstValue, secondValue) { return firstValue * secondValue; } function powerTo(firstValue, secondValue) { return firstValue ^ secondValue; } switch (operation) { case "+": addition(); break; case "-": subtraction(); break; case "/": division(); break; case "*": multiplication(); break; case "^": powerTo(); break; default: console.log("No operation inputted"); break; } console.log(`${firstValue} ${operation} ${secondValue} =`)

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Asigne el resultado de llamar a la función de operación a una variable, luego muéstrelo en la consola.

También necesita pasar argumentos a todas las funciones.

Y debe convertir la entrada en un número antes de usarla en los cálculos. La mayoría de los operadores aritméticos lo convertirán automáticamente, pero + hará la concatenación de cadenas.

 let firstValue = Number(prompt("Enter a number")); let secondValue = Number(prompt("Enter a second number")); let operation = prompt("How should these numbers interact"); function addition(firstValue, secondValue) { return firstValue + secondValue; } function subtraction(firstValue, secondValue) { return firstValue - secondValue; } function division(firstValue, secondValue) { return firstValue / secondValue; } function multiplication(firstValue, secondValue) { return firstValue * secondValue; } function powerTo(firstValue, secondValue) { return firstValue ^ secondValue; } let result; switch (operation) { case "+": result = addition(firstValue, secondValue); break; case "-": result = subtraction(firstValue, secondValue); break; case "/": result = division(); break; case "*": result = multiplication(firstValue, secondValue); break; case "^": result = powerTo(firstValue, secondValue); break; default: console.log("No operation inputted"); break; } console.log(`${firstValue} ${operation} ${secondValue} = ${result}`)

over 4 years ago · Santiago Trujillo Denunciar

0

Hay un par de cosas mal con su código. Primero, debe guardar el resultado de la llamada de función en una variable, segundo, debe pasar los argumentos a la función y, finalmente, en JavaScript, los exponentes se realizan utilizando el operador ** , no el operador ^ , ^ se usa para un xor bit a bit.

Aquí está su código cuando se solucione:

 let firstValue = prompt("Enter a number"); let secondValue = prompt("Enter a second number"); let operation = prompt("How should these numbers interact"); function addition(firstValue, secondValue) { return firstValue + secondValue; } function subtraction(firstValue, secondValue) { return firstValue - secondValue; } function division(firstValue, secondValue) { return firstValue / secondValue; } function multiplication(firstValue, secondValue) { return firstValue * secondValue; } function powerTo(firstValue, secondValue) { return firstValue ** secondValue; } let result; switch(operation) { case "+" : result = addition(firstValue, secondValue); break; case "-": result = subtraction(firstValue, secondValue); break; case "/": result = division(firstValue, secondValue); break; case "*": result = multiplication(firstValue, secondValue); break; case "^": result = powerTo(firstValue, secondValue); break; default: console.log("No operation inputted"); break; } console.log(`${firstValue} ${operation} ${secondValue} = ${result}`);

Sin embargo, en este caso siento que separar las operaciones en funciones es innecesario. Aquí hay una versión limpia:

 let firstValue = prompt("Enter a number"); let secondValue = prompt("Enter a second number"); let operation = prompt("How should these numbers interact"); let result; switch(operation) { case "+" : result = firstValue + secondValue; break; case "-": result = firstValue - secondValue; break; case "/": result = firstValue / secondValue; break; case "*": result = firstValue * secondValue; break; case "^": result = firstValue ** secondValue; break; default: console.log("No operation inputted"); break; } console.log(`${firstValue} ${operation} ${secondValue} = ${result}`);

over 4 years ago · Santiago Trujillo Denunciar

0

EDITAR: todos respondieron al mismo tiempo con lo mismo, así que pensé en darle vida a mi respuesta con un código inseguro

 const firstValue = prompt("Enter a number"); const secondValue = prompt("Enter a second number"); const operation = prompt("How should these numbers interact"); const expression = `${firstValue} ${operation} ${secondValue}`; const result = eval(expression); console.log(`${expression} = ${result}`);

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