大家好,这是我的第一学期,我对javascript很陌生。几个小时后我就要交作业了,我一直在绞尽脑汁想弄清楚为什么我的代码不能显示实际的函数结果。这是作业
**编写一个JavaScript程序,实现了一个功能齐全的,五个特性(加、减、乘、除、模/余)计算器。
当我运行我的代码时,它的行为就像我没有任何输入值,并返回我的警报语句与0或NaN。MY CODE SO FAR
var input1 = 0;
var input2 = 0;
var again = true;
function main()
{
while (again === true)
{
inputnum1();
inputnum2();
operator();
}
}
function inputnum1()
{
var input1 = 0;
input1 = parseInt(prompt("Input first number: ", "1"));
while ((isNaN(input1)) || (input1 < 1))
{
input1 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
}
}
function inputnum2()
{
var input2 = 0;
input2 = parseInt(prompt("Input second number: ", "1"));
while ((isNaN(input2)) || (input2 < 1))
{
input1 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
input2 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
}
}
function operator()
{
var option = 0;
var string = "";
string += "nEnter 0 to terminate calculator: ";
string += "nEnter 1 for addition: ";
string += "nEnter 2 for subtraction : ";
string += "nEnter 3 for multiplication: ";
string += "nEnter 4 for division: ";
string += "nEnter 5 for modulo/remainder: ";
option = parseInt(prompt(string, "1"));
while ((isNaN(option)) || (option < 0) || (option >5))
{
option = parseInt(prompt(string, "1"));
}
chosenop(option);
}
function chosenop(option)
{
switch (option)
{
case 0:
alert ("Terminated successfully");
again = false;
break;
case 1:
addition();
break;
case 2:
subtract();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
modulo();
break;
}
}
function addition()
{
var sum = input1+input2;
alert ("The sum is: " + sum);
}
function subtract()
{
var diff = input1-input2;
alert("The difference is: " + diff);
}
function multiply()
{
var prod = input1*input2;
alert ("The product is: " + prod);
}
function divide()
{
var quot = (input1 + 0.0)/(input2 + 0.0);
alert ("The quotient is: " + quot);
}
function modulo()
{
var mod = input1%input2;
alert ("The modulo is: " + mod);
}
main();
首先,我想让您通读一下本指南。
现在,你的任务。inputnum2()
中的这一行:
input1 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
superflous。
在代码的开头,您已经定义了变量input1
和input2
。然而,在inputnum1()
和inputnum2()
中,您执行var input1 = 0
和var input2 = 0
,这将它们的范围限制在function
块。这里有一篇关于范围的维基百科文章。所以,把它们去掉。
你的代码应该是这样的:
var input1 = 0;
var input2 = 0;
var again = true;
function main()
{
while (again === true)
{
inputnum1();
inputnum2();
operator();
}
}
function inputnum1()
{
input1 = parseInt(prompt("Input first number: ", "1"));
while ((isNaN(input1)) || (input1 < 1))
{
input1 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
}
}
function inputnum2()
{
input2 = parseInt(prompt("Input second number: ", "1"));
while ((isNaN(input2)) || (input2 < 1))
{
input2 = parseInt(prompt("Numeric values starting at 1 only: " , "1"));
}
}
function operator()
{
var option = 0;
var string = "";
string += "nEnter 0 to terminate calculator: ";
string += "nEnter 1 for addition: ";
string += "nEnter 2 for subtraction : ";
string += "nEnter 3 for multiplication: ";
string += "nEnter 4 for division: ";
string += "nEnter 5 for modulo/remainder: ";
option = parseInt(prompt(string, "1"));
while ((isNaN(option)) || (option < 0) || (option >5))
{
option = parseInt(prompt(string, "1"));
}
chosenop(option);
}
function chosenop(option)
{
switch (option)
{
case 0:
alert ("Terminated successfully");
again = false;
break;
case 1:
addition();
break;
case 2:
subtract();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
modulo();
break;
}
}
function addition()
{
var sum = input1+input2;
alert ("The sum is: " + sum);
}
function subtract()
{
var diff = input1-input2;
alert("The difference is: " + diff);
}
function multiply()
{
var prod = input1*input2;
alert ("The product is: " + prod);
}
function divide()
{
var quot = (input1 + 0.0)/(input2 + 0.0);
alert ("The quotient is: " + quot);
}
function modulo()
{
var mod = input1%input2;
alert ("The modulo is: " + mod);
}
main();