如何在控制台打印返回值?
我有一个课程任务是用 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 技术交流群。

发布评论
评论(4)
雾里花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}`);
青萝楚歌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}`);
玩世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);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
将调用运算函数的结果赋值给变量,然后将其显示在控制台中。
您还需要将参数传递给所有函数。
在计算中使用之前,您应该将输入转换为数字。大多数算术运算符会自动转换它,但
+
会进行字符串连接。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.