基本Javascript菜单程序中的函数有问题

基本Javascript菜单程序中的函数有问题,javascript,Javascript,在我的Comp-Sci课程介绍中,教授要求我们将函数实现为一个主要函数,我们已经用do-while和For循环、switch语句和基本条件编程完成了一项任务。但是,他没有很好地指导我们如何做到这一点 这就是原始赋值中没有函数的代码的外观 <html> <head> </head> <body> <script> var menu_choice; do { menu_choice = 1

在我的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>

变量菜单选择;
做{
menu\U choice=1*提示(“----菜单----\n\n”+
“1.添加\n”+
“2.乘法\n”+
“3.求幂\n”+
“4.字符串连接\n”+
“5.逻辑AND(&&)\n”+
“6.逻辑或(| |)\n”+
“7.退出\n\n”+
“输入您的选择(1-7):”;
开关(菜单选择){
案例1:
var迭代次数=1*提示(“输入要添加的数字数量”);
var sum=1*提示(“输入一个数字:”);
对于(变量i=1;i
”; 打破 案例2: var迭代次数=1*提示(“输入要乘法的数字数量”); var乘积=1;
对于(var i=1;i来说,知道代码在做什么很重要。要回答您的问题,我们真正需要查看的唯一代码是
sum\u 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);
2) 要求用户输入另一个数字,将其乘以1,将其添加到变量
result
并返回

return result += 1*prompt("Enter a number:");
因此,每次运行
for
循环时,您的代码都会取两个数字,将它们相加,并将它们存储在
final_result
中,然后在最后提醒输入的最后两个数字的总和

这听起来像是您希望代码执行的操作,即获取
input()
将其存储在
result
中,然后运行一个循环,获取新的输入值并将其添加到
result

您的
sum\u 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;
}
或者更长的版本,几乎完全相同:

var iterations = iterations("add");
var sum = sum_plus(iterations);
alert(sum);
确保您真正了解这是如何工作的,不要只是复制我的代码。函数和循环是现代代码的组成部分。没有它们,您将一事无成。如果您对发生的事情有任何疑问,请随时提问。

功能帮助:


关于
循环的
帮助:

这正是我想要做的。我正试图根据你告诉我的内容想出一个解决方案,但不幸的是,我什么都想不出来。我已经为此工作了好几个小时,因为我们的老师几乎不看函数,随意看循环,所以我不得不自学。我我认为这可能与sum_plus函数的语法有关,而不是for循环,但我不完全确定。@MaxSilbert认为你也可以在那里投票吗?:)非常感谢!我不会只是复制代码然后继续,我肯定会浏览你提供的那些页面。我想我只需要看看解决方案并然后重新分析代码以学习它(因为我从来没有得到过正确的课程)。再次感谢,我现在要处理菜单中的其他案例。很抱歉,我试图提升它,但我没有足够的信誉点。一旦我得到足够的分数,我会回来并提升它!:)再次感谢。
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);