对于我的Comp Sci课程,教授要求我们将函数实现为一个主函数,用于我们已经用do-while和for循环,switch语句和基本条件编程完成的赋值。但是,他没有告诉我们如何实现这一目标。
这是原始作业中的代码在没有函数的情况下的样子。
<html>
<head>
</head>
<body>
<script>
var menu_choice;
do {
menu_choice = 1 * prompt("-----Menu------\n\n" +
"1. Addition\n" +
"2. Multiplication\n" +
"3. Expenentiation\n" +
"4. String Concatenation\n" +
"5. Logical AND (&&)\n" +
"6. Logical OR (||)\n" +
"7. Quit\n\n" +
"Enter your choice (1-7):");
switch (menu_choice) {
case 1:
var iterations = 1*prompt("Enter number of numbers to add");
var sum = 1*prompt("Enter a number:");
for(var i = 1;i < iterations; i++){
sum += 1*prompt("Enter a number:");}
alert("The sum is " + sum);
document.write("The sum is " + sum + "<br><br>");
break;
case 2:
var iterations = 1*prompt("Enter number of numbers to multiply");
var product = 1;
for(var i = 1;i <= iterations; i++){
product *= 1*prompt("Enter a number:");}
alert("The product is " + product);
document.write("The product is " + product + "<br><br>");
break;
case 3:
var iterations = 1*prompt("Enter number of numbers to exponentiate");
var result = 1*prompt("Enter a number");
for(var i = 1;i < iterations; i++){
result = Math.pow(result, 1*prompt("Enter a number"));}
alert("The exponentiation is " + result);
document.write("The exponentiation is " + result + "<br><br>");
break;
case 4:
var iterations = 1*prompt("Enter the number of strings to concatenate:");
var result = prompt("Enter a string");
for(var i = 1;i < iterations; i++){
result += prompt("Enter a string");}
alert("The concatenated string is " + result);
document.write("The concatenated string is " + result + "<br><br>");
break;
case 5:
var iterations = 1*prompt("Enter the number of booleans to AND:");
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var result = string;
if (result === "true")
var result = true;
else
var result = false;
for (var i = 1; i < iterations; i++){
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var newbool = string;
if (newbool === "true")
var newbool = true;
else
var newbool = false;
var result = (result && newbool)
}
alert("The ANDing of the booleans is: " + result);
document.write("The ANDing of the booleans is: " + result + "<br><br>");
break;
case 6:
var iterations = 1*prompt("Enter the number of booleans to OR:");
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var result = string;
if (result === "true")
var result = true;
else
var result = false;
for (var i = 1; i < iterations; i++){
var string = prompt("Enter a boolean (true or false):").toLowerCase();
var newbool = string;
if (newbool === "true")
var newbool = true;
else
var newbool = false;
var result = (result || newbool)
}
alert("The ORing of the booleans is: " + result);
document.write("The ORing of the booleans is: " + result + "<br><br>");
break;
case 7:
document.write("Quitting the program...");
break;
default:
document.write("Error: invalid input (must be 1-7)<br><br>");
}
}
while (menu_choice !== 7)
</script>
</body>
</html>
现在我只是尝试处理案例1,但我想我已经开始为'+ ='运算符编写函数了。在测试中(使用JSrunner)我只使用了两个数字的函数。
以下是我为案例1测试的代码。
var number_of_numbers, final_result;
var iterations = function (op)
{
return 1*prompt("Enter the number of numbers to " + op + ":");
};
var input = function()
{
return 1*prompt("Enter a number:");
};
var sum_plus = function()
{
var result = input();
return result += 1*prompt("Enter a number:");
};
number_of_numbers = iterations("add");
for(var i = 1;i < number_of_numbers; i++){
final_result = sum_plus();};
alert("The sum is " + final_result);
是否可以将for循环放在sum_plus函数中?或者更好地调用for循环中的函数?
非常感谢任何帮助。
答案 0 :(得分:0)
了解您的代码正在做什么非常重要。要回答您的问题,我们真正需要查看的唯一代码是sum_plus
函数
var sum_plus = function() {
var result = input();
return result += 1*prompt("Enter a number:");
};
运行它的代码:
number_of_numbers = iterations("add");
for(var i = 1;i < number_of_numbers; i++) {
final_result = sum_plus();
}
alert("The sum is " + final_result);
现在,此代码将在每次迭代时调用sum_plus()
。 sum_plus()
将:
1)向用户询问一个数字并将其存储在变量result
var result = input();
2)询问用户另一个号码,乘以1,将其添加到变量result
并返回。
return result += 1*prompt("Enter a number:");
因此,每次for
循环运行时,您的代码将获取两个数字,添加它们并将它们存储在final_result
中,然后最后将提醒最后两个数字的总和输入
这听起来就像您希望代码执行的操作一样,input()
将其存储在result
中,然后运行一个带有新输入值的循环并将其添加到result
。< / p>
您的sum_plus
函数应如下所示:
var sum_plus = function(iteration_count) {
// Take an input and store it in the variable `result`
var result = input();
// Run a loop `iteration_count` times
for (var i = 0; i < iteration_count; i++) {
// Every time the loop runs, take an input and add it to result
result += input(); // This is the same as `result = result + input();
}
// Return the value
return result;
}
现在您可以使用以下方法调用该函数:
alert(sum_plus(iterations("add")));
或者更长的版本几乎完全相同:
var iterations = iterations("add");
var sum = sum_plus(iterations);
alert(sum);
确保您真正了解其工作原理,不要只复制我的代码。函数和循环是现代代码的构建块。没有他们你就无法获得任何成就。如果您对发生的事情有任何疑问,请随时提出。