【问题标题】:Trying to get values from different arrays to average them试图从不同的数组中获取值来平均它们
【发布时间】:2021-02-08 19:15:01
【问题描述】:

我正在完成这项任务,主要是从用户那里收集数据以输入他们学生的成绩。系统提示我存储学生姓名和每个学生的 4 个成绩,最后将它们平均。我遇到了一个问题,不确定如何将这些值加起来并取平均值。

这是我到目前为止所写的:

let lab1 = [];
    let lab2 = [];
    let lab3 = [];
    let lab4 = [];
    let stuName = [];
    let grades = [];
    
    
    let grade = 0;
    let tGrade = 0;
    
    
    
    do {
        let tName = prompt("Enter Student Name: ");
        stuName[stuName.length] = tName;


    //prompting user for a grade and converting it LAB1
    tGrade = prompt("Input Grade for lab 1: ");
    grade = parseInt(tGrade);
    //If grade is valid, then add it to the array 
    if (grade >= 0 && grade <= 100) {
        //info stored in the array
        lab1[lab1.length] = grade;
    }

    //prompting user for a grade and converting it LAB2
    tGrade = prompt("Input Grade for lab 2: ");
    grade = parseInt(tGrade);
    //If grade is valid, then add it to the array 
    if (grade >= 0 && grade <= 100) {
        //info stored in the array
        lab2[lab2.length] = grade;
    }

    //prompting user for a grade and converting it LAB3
    tGrade = prompt("Input Grade for lab 3: ");
    grade = parseInt(tGrade);
    //If grade is valid, then add it to the array 
    if (grade >= 0 && grade <= 100) {
        //info stored in the array
        lab3[lab3.length] = grade;
    }

    //prompting user for a grade and converting it LAB4
    tGrade = prompt("Input Grade for lab 4: ");
    grade = parseInt(tGrade);
    //If grade is valid, then add it to the array 
    if (grade >= 0 && grade <= 100) {
        //info stored in the array
        lab4[lab4.length] = grade;
    }

    //giving user option to end inputs
    tGrade = prompt("Do you want continue? -1 to exit: ", "-1");
    grade = parseInt(tGrade);


} while (grade != -1);  //loop escape

【问题讨论】:

  • 数组并不是这里最好的数据结构。我认为您应该将其存储在具有属性名称和等级的对象中
  • 好吧,到目前为止,这段代码似乎工作正常。您有一个包含 4 个值的数组,您所要做的就是 get the average 这些值。你快到了!

标签: javascript arrays average


【解决方案1】:

这里有一个解决方案,将学生及其成绩存储在一个对象中,并将该对象存储在一个数组中。这比拥有 5 个不同的数组(如

)要容易一些
let lab2 = [];
let lab3 = [];
let lab4 = [];
let stuName = [];
let grades = [];

要计算平均值,您可以遍历这些对象并构建等级数组的总和,然后除以它的长度

objStore.forEach((x) => {
      let sum = x.grades.reduce((acc, el) => {
      acc += el;
      return acc;
      },0);
      console.log(sum / x.grades.length)
    })

const objStore = [];

let grade = 0;
let tGrade = 0;
let temp;



do {
  temp = {};
  let tName = prompt("Enter Student Name: ");
  temp["name"] = tName;
  temp["grades"] = [];

  //prompting user for a grade and converting it LAB1
  tGrade = prompt("Input Grade for lab 1: ");
  grade = parseInt(tGrade);
  //If grade is valid, then add it to the array 
  if (grade >= 0 && grade <= 100) {
    //info stored in the array
    temp.grades.push(grade);
  }

  //prompting user for a grade and converting it LAB2
  tGrade = prompt("Input Grade for lab 2: ");
  grade = parseInt(tGrade);
  //If grade is valid, then add it to the array 
  if (grade >= 0 && grade <= 100) {
    //info stored in the array
    temp.grades.push(grade);
  }

  //prompting user for a grade and converting it LAB3
  tGrade = prompt("Input Grade for lab 3: ");
  grade = parseInt(tGrade);
  //If grade is valid, then add it to the array 
  if (grade >= 0 && grade <= 100) {
    //info stored in the array
    temp.grades.push(grade);
  }

  //prompting user for a grade and converting it LAB4
  tGrade = prompt("Input Grade for lab 4: ");
  grade = parseInt(tGrade);
  //If grade is valid, then add it to the array 
  if (grade >= 0 && grade <= 100) {
    //info stored in the array
    temp.grades.push(grade);
  }

  //giving user option to end inputs
  tGrade = prompt("Do you want continue? -1 to exit: ", "-1");
  grade = parseInt(tGrade);

  objStore.push(temp);
} while (grade != -1); //loop escape

console.log(objStore);
// here we can now calculate the average
objStore.forEach((x) => {
      let sum = x.grades.reduce((acc, el) => {
      acc += el;
      return acc;
      },0);
      console.log(sum / x.grades.length)
    })

【讨论】:

    【解决方案2】:

    我假设您想要的是每个学生的平均值,而不是每个实验室的平均值。我没有提供完整的解决方案,因为我假设您希望有一个指针,您可以从中找出更多解决方案。

    您应该能够循环遍历 stuName 数组,这样您就可以使用 stuName 数组中每个项目的索引来查找 labx 数组中的相应值。

    有很多方法可以做到这一点,但for loopArray.forEachArray.map 应该允许您这样做。 (对于 Array.forEach 和 Array.map)回调的第二个参数是数组中项目的当前索引。

    Array.forEach 的使用示例:

    const students = ['Neera', 'Erik', 'Dolly'];
    const student_score = [2, 5, 6];
    
    students.forEach((name, index) => console.log('Name: ' + name + ' has score ' + student_score[index]));

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 2011-02-03
      • 2020-06-15
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多