Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}`)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading