1

I'm working on this assignment where basically I'm gathering data from users to input their student's grades. I was prompted to store the student names and 4 of each student's grades and average them out in the end. I have hit a hole and unsure how to get these values to add them up and average them.

Here's what I have written so far:

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
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • Array isn't the best datastructure here. I think you should store it in an object with the propertys name and grades – Aalexander Feb 08 '21 at 19:21
  • Well, this code seems to be working so far. You have an array holding 4 values, all you have to do is to [get the average](https://stackoverflow.com/questions/10359907/how-to-compute-the-sum-and-average-of-elements-in-an-array) of these values. You're almost there! – Jeremy Thille Feb 08 '21 at 19:23

2 Answers2

0

Here a solution storing the students with their grades in an object and storing this object in an array. This makes it a bit easier than having 5 different arrays like

let lab2 = [];
let lab3 = [];
let lab4 = [];
let stuName = [];
let grades = [];

To calculate the average you can go then over these objects and build the sum of the grades array and divide it by its length

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)
    })
Aalexander
  • 4,987
  • 3
  • 11
  • 34
0

I'm assuming you want the average of each student rather than the average of each lab. I've not provided a complete solution because I assume that you'd prefer to have a pointer from which you can figure out more of a solution.

You should be able to loop though the stuName array in such a way that you can use the index of each item in the stuName array to find the corresponding values in the labx arrays.

There are many ways to do this, but a for loop, Array.forEach or Array.map should allow you to do this. (For Array.forEach and Array.map) the callback's second argument is the current index of the item in the array.

Example usage of 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]));
Ben Stephens
  • 3,303
  • 1
  • 4
  • 8