如何在控制台打印返回值?

发布于 01-20 19:25 字数 1116 浏览 2 评论 0原文

我有一个课程任务是用 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;
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟酒忠诚2025-01-27 19:25:04

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

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

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

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

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

雾里花2025-01-27 19:25:04

您的代码有几件事。
首先,您需要将函数调用的结果保存到变量,其次,您需要使用**操作员而不是^将参数传递给函数,最后在JavaScript指数中完成。操作员,^用于位XOR。

修复后,这是您的代码:

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

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

青萝楚歌2025-01-27 19:25:04

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

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

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

玩世2025-01-27 19:25:04

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

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

Another way of solving the problem. More complicated, but safe and clean.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文