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

232
Vistas
How do I print the return value on the console?

I've got a course task to make a calculator in Javascript. I've got the functions for the different operators and the case-switch but I'm struggling with how to print the return values of the functions. Here's the code I've got so far:

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

Assign the result of calling the operation function to a variable, then display that in the console.

You also need to pass arguments to all the functions.

And you should convert the input to a number before using it in calculations. Most of the arithmetic operators will convert it automatically, but + will do string concatenation.

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

There are a couple of things wrong with your code. First you need to save the result of the function call to a variable, second you need to pass the arguments to the function and finally in javascript exponents are done using the ** operator not the ^ operator, ^ is used for a bitwise xor.

Here is your code when it is fixed:

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}`);

However in this case I feel that separating the operations into functions is unnecessary. Here is a cleaned up version:

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

EDIT: everyone answered at the same time with the same thing so I thought I would spice my answer up with some unsafe code

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