【问题标题】:How do I add the data stored in an Array?如何添加存储在数组中的数据?
【发布时间】:2015-07-28 17:36:03
【问题描述】:

如何在输入时将提示输入的数据添加到数组中,然后除以数组大小为 4 的数量?我真正需要帮助的唯一一件事是将输入的数据相加并将其除以显示平均值。

<html>
<body>
<script type="text/javascript">

// declare variables and constants
   var SIZE = 4   // array size
   var gradeArray // array variable
   var gradeAvg  // grade average variable
   var avgCalc = parseFloat(avgCalc); // variable used to store avg grade
   var letGrad = "" // variable used to store Letter Grade
   var userInput  // variable used for while function
   var index  // loop variable
   var BR = "<br></br>" // page break
   var ES = ""  // empty string
   var semi = ":"
// Create Array
   gradeArray = new Array(SIZE);   
}

// Create Loop structure for user grade input and ask if input is correct
   while (userInput != "Y") {
   for (index = 0; index < SIZE; index++) {  
      gradeArray[index] = prompt("Enter your grades # " + (index + 1) + ":" + ES);
}

// Create display function for loop structure, Displays user input of grades

   document.write("Grades Entered:" + BR);
   for (index = 0; index < SIZE; index++) {   
      document.write("The grades you entered are: ");
      document.write(gradeArray[index] + BR);
}
   userInput = prompt("Are the these grades correct? (Y/N)");   
   if (userInput == "Y") {
     document.write("Thank you!" + BR);
}
}
// Create selection structure. This structure gives the average a letter grade
   if (avgCalc >= 90) {
     letGrad = "A";
}
   else if (avgCalc >= 80) {
     letGrad = "B";
}
   else if (avgCalc >= 70) {
     letGrad = "C";
}
   else if (avgCalc >= 60) {
     letgrad = "D";
}
   else if (avgCalc < 60) {
     letGrad = "F";

}

// Display results -- display's results
   document.write("Your average is: " + avgCalc + semi + letGrad + BR);
   document.write("Thank you for using Average Calculator!");

</script>
</body>
</html>

【问题讨论】:

  • 你能把所有的代码都包含进去吗,现在你少了一个大括号,我们不知道gradeArray是在哪里创建的。
  • 如果您所做的只是计算平均值,则无需将值存储在数组中。
  • 我不只是计算平均值。在那种情况下,id 只需将输入放入单独的变量中,然后使用函数?

标签: javascript arrays add


【解决方案1】:

您可以使用数组reduce() 方法。它需要一个累加器和一个值,将它们相加并存储到您的累加器中。像这样:

var gradeSum = gradeArray.reduce(function(total, n){ 
  return total + n
});

这将为您提供存储在数组中的所有成绩的总和。 你只需要这样划分它

var med = gradeSum/gradeSum.length

现在您的 med 变量将具有您想要的值!

在此处进一步了解 Array reduce 方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

【讨论】:

    【解决方案2】:

    我明白了!就像我知道有一个更简单的方法。我是一个业余程序员。你们比我先进很多,所以你的例子(对你来说很简单)对我来说有点先进。

    // Welcome user
       document.write("Welcome to Grade Average Calculator" + BR);
    // Create Array
       gradeArray = new Array(SIZE);   
    
    // Create Loop structure for user grade input and ask if input is correct
       while (userInput != "Y") {
       for (index = 0; index < SIZE; index++) { 
          gradeArray[index] = prompt("Enter your grades # " + (index + 1) + ":" + ES);
          gradeSum = +gradeArray[0] + +gradeArray[1] + +gradeArray[2] + +gradeArray[3];
          avgCalc = gradeSum / 4;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2018-02-08
      相关资源
      最近更新 更多