我无法在 javascript 中显示计算器的结果,其余代码不起作用
I can't display the result of a calculator in javascript, the rest of the code doesn't work
您好,我正在尝试创建一个计算器,但无法显示结果。我首先尝试将 return 存储在一个变量中,然后我将我的函数存储在一个变量中我做了一个 alert() 然后我在我的切换条件之后用管道为我的不同函数做了一个警报但是没有任何效果。
这是我编写的代码。你能告诉我如何显示结果吗?
let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
let firstNumber = parseInt(prompt('write your first number'));
let secondNumber = parseInt(prompt('write your second number'));
let result;
do {
isNaN()
} while (firstNumber, secondNumber);
result = function addition() {
return firstNumber + secondNumber;
}
result = function multiplication() {
return firstNumber * secondNumber;
}
result = function soustraction() {
return firstNumber - secondNumber;
}
result = function division() {
return firstNumber / secondNumber;
}
switch (choice) {
case "1":
addition();
break;
case "2":
multiplication();
break;
case "3":
soustraction();
break;
case "4":
division();
break;
default:
'This number isn\'t in the list'
}
alert(result(firstNumber, secondNumber));
try {
switch (division) {
case secondNumber = 0:
alert('You can\'t split by 0');
}
} catch {
console.error(error.stack);
}
我重写了一些东西以使其正常工作。这是我对您尝试的方法的处理方法:
let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
function checkFirstNumber(anotherTry = null) {
let bad = true;
let firstIn;
if (anotherTry == null) {
firstIn = prompt('write your first number')
} else {
firstIn = prompt('You did not enter a valid number. Please write your first number.')
}
bad = isNaN(firstIn);
if (bad != false) {
checkFirstNumber(true);
} else {
return parseFloat(firstIn);
}
}
function checkSecondNumber(anotherTry = null) {
let bad = true;
let firstIn;
if (anotherTry == null) {
firstIn = prompt('write your second number')
} else {
firstIn = prompt('You did not enter a valid number. Please write your second number.')
}
bad = isNaN(firstIn);
if (bad != false) {
checkFirstNumber(true);
} else {
return parseFloat(firstIn);
}
}
let firstNumber = checkFirstNumber();
let secondNumber = checkSecondNumber();
let result;
// do {
// isNaN()
// } while (firstNumber, secondNumber);
function addition(inFirst, inSecond) {
return inFirst + inSecond;
}
function multiplication(inFirst, inSecond) {
return inFirst * inSecond;
}
function soustraction(inFirst, inSecond) {
return inFirst - inSecond;
}
function division(inFirst, inSecond) {
return inFirst / inSecond;
}
switch (choice) {
case "1":
result = addition(firstNumber, secondNumber);
break;
case "2":
result = multiplication(firstNumber, secondNumber);
break;
case "3":
result = soustraction(firstNumber, secondNumber);
break;
case "4":
if (secondNumber == 0) {
result = 'You can\'t divide by 0. Please try again.';
} else {
result = division(firstNumber, secondNumber);
}
break;
default:
'This number isn\'t in the list'
}
alert(result);
location.reload();
您好,我正在尝试创建一个计算器,但无法显示结果。我首先尝试将 return 存储在一个变量中,然后我将我的函数存储在一个变量中我做了一个 alert() 然后我在我的切换条件之后用管道为我的不同函数做了一个警报但是没有任何效果。
这是我编写的代码。你能告诉我如何显示结果吗?
let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
let firstNumber = parseInt(prompt('write your first number'));
let secondNumber = parseInt(prompt('write your second number'));
let result;
do {
isNaN()
} while (firstNumber, secondNumber);
result = function addition() {
return firstNumber + secondNumber;
}
result = function multiplication() {
return firstNumber * secondNumber;
}
result = function soustraction() {
return firstNumber - secondNumber;
}
result = function division() {
return firstNumber / secondNumber;
}
switch (choice) {
case "1":
addition();
break;
case "2":
multiplication();
break;
case "3":
soustraction();
break;
case "4":
division();
break;
default:
'This number isn\'t in the list'
}
alert(result(firstNumber, secondNumber));
try {
switch (division) {
case secondNumber = 0:
alert('You can\'t split by 0');
}
} catch {
console.error(error.stack);
}
我重写了一些东西以使其正常工作。这是我对您尝试的方法的处理方法:
let choice = prompt('What operation would you like to do?\n \n 1: Addition\n 2:Soustraction\n 3: Multiplication\n 4: Division');
function checkFirstNumber(anotherTry = null) {
let bad = true;
let firstIn;
if (anotherTry == null) {
firstIn = prompt('write your first number')
} else {
firstIn = prompt('You did not enter a valid number. Please write your first number.')
}
bad = isNaN(firstIn);
if (bad != false) {
checkFirstNumber(true);
} else {
return parseFloat(firstIn);
}
}
function checkSecondNumber(anotherTry = null) {
let bad = true;
let firstIn;
if (anotherTry == null) {
firstIn = prompt('write your second number')
} else {
firstIn = prompt('You did not enter a valid number. Please write your second number.')
}
bad = isNaN(firstIn);
if (bad != false) {
checkFirstNumber(true);
} else {
return parseFloat(firstIn);
}
}
let firstNumber = checkFirstNumber();
let secondNumber = checkSecondNumber();
let result;
// do {
// isNaN()
// } while (firstNumber, secondNumber);
function addition(inFirst, inSecond) {
return inFirst + inSecond;
}
function multiplication(inFirst, inSecond) {
return inFirst * inSecond;
}
function soustraction(inFirst, inSecond) {
return inFirst - inSecond;
}
function division(inFirst, inSecond) {
return inFirst / inSecond;
}
switch (choice) {
case "1":
result = addition(firstNumber, secondNumber);
break;
case "2":
result = multiplication(firstNumber, secondNumber);
break;
case "3":
result = soustraction(firstNumber, secondNumber);
break;
case "4":
if (secondNumber == 0) {
result = 'You can\'t divide by 0. Please try again.';
} else {
result = division(firstNumber, secondNumber);
}
break;
default:
'This number isn\'t in the list'
}
alert(result);
location.reload();