如何在控制台上打印 return 值?

How do I print the return value on the console?

我有一个课程任务是在 Javascript 中制作一个计算器。我有针对不同运算符和大小写切换的函数,但我正在为如何打印函数的 return 值而苦苦挣扎。这是我目前得到的代码:

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

将调用运算函数的结果赋值给一个变量,然后在控制台显示。

您还需要将参数传递给所有函数。

并且在计算中使用之前,您应该将输入转换为数字。大多数算术运算符会自动转换它,但 + 会进行字符串连接。

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

编辑:每个人都同时回答了同样的问题,所以我想我会用一些不安全的代码来增加我的回答

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

您的代码有几处错误。 首先,您需要将函数调用的结果保存到变量中,其次,您需要将参数传递给函数,最后在 javascript 中,指数是使用 ** 运算符而不是 ^ 运算符,^ 用于按位异或。

修复后的代码如下:

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

然而在这种情况下,我觉得将操作分成功能是不必要的。这是清理后的版本:

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

另一种解决问题的方法。更复杂,但安全干净。

function getNumber(message) {
    let number;
    do {
        number = prompt(message);
        if (isNaN(number)) {
            alert(`${number} is not a number, try again.`);
        } else break;
    } while (true);
    return parseInt(number);
}

function add(offset, x) {
    return offset + x;
}

const operations = {
    "+": add,
};

const firstNumber = getNumber("Enter the first number");
const secondNumber = getNumber("Enter the second number");

let operation;
do {
    operation = prompt("How should these numbers interact");
    if (!operations[operation]) {
        alert(operation + " is not supported");
    } else break;
} while (true);

const calculate = operations[operation];

const result = calculate(firstNumber, secondNumber);
alert("Answer is " + result);