hughm-01
hughm-01

Reputation: 35

Running a function on an Array of Arrays - js

I am trying to take the average of 3 grades for three student (stored in an array of arrays), and then run those averages through a function with an else if statement to check whether the average grades are each and A,B or C.

I would prefer not to have to make a separate function with an else if for each students average (so I would know how to scale this to more than 3 inputs), and I am not sure how I can index the averageGrades array in the function so that I can console.log each element (student) of the averageGrades array and have the else if statement evaluate that particular element (student).

I also tried making an averageGrade variable for each student so that the averageGrades array had single values and not a full equation but ran into the same problem.

var studentGrades = [
  [80, 90, 94],
  [80, 90, 94],
  [80, 90, 94]
]
var studentAvgerages = [
  (studentGrades[0][0] + studentGrades[0][1] + studentGrades[0][2]) / 3,
  (studentGrades[1][0] + studentGrades[1][1] + studentGrades[1][2]) / 3,
  (studentGrades[2][0] + studentGrades[2][1] + studentGrades[2][2]) / 3
]
for (var i = 0; i <= studentAvgerages.length; i++) {
  function evalGrades(studentAvgerages[i]) {
    if (studentAvgerages[i] >= 90) {
      return "A"
    } else if ((studentAvgerages[i] >= 80) && (studentAvgerages[i] < 90)) {
      return "B"
    } else if ((studentAvgerages[i] >= 70) && (studentAvgerages[i] < 80)) {
      return "C"
    } else {
      return "Failed"
    }
  }
}

console.log(evalGrades(studentAvgerages[0]))
console.log(evalGrades(studentAvgerages[1]))
console.log(evalGrades(studentAvgerages[2]))

Upvotes: 1

Views: 216

Answers (4)

TCooper
TCooper

Reputation: 1900

Thought I knew what you were looking for, less sure now, but hope this helps a little, somehow? As others have shown, there are some one liners to arrive at your average, if that's what you want.

var studentGrades = [
  [80, 90, 94],
  [80, 90, 94],
  [80, 90, 94]
]

for(var i=0; i < studentGrades.length; i++){
    var avg = 0;
    for(var j=0; j < studentGrades[i].length; j++){
        avg += studentGrades[i][j];
    }
    avg = avg/studentGrades[i].length;
    switch(true){
        case (avg >= 90):
            console.log("A");
            break;
        case (avg >= 80):
            console.log("B");
            break;
        case (avg >= 70):
            console.log("C");
            break;
        case (avg >= 60):
            console.log("D");
            break;
        default:
            "Failed";
            break;
    }
}

I prefer switch...case for tasks like this a lot of times, but don't forget to take into account performance. On an array of 20,000 sets of 200 student grades, might be worth using if/else to maintain speed of page. See this answer for more details.

Upvotes: 1

54ka
54ka

Reputation: 3589

Try this. I hope I've been helpful.

var studentGrades = [
  [80, 90, 94],
  [80, 90, 94],
  [80, 90, 94]
]

for (var i = 0; i < studentGrades.length; i++) {

    var average = studentGrades[i].reduce((a, b) => a + b, 0) / studentGrades[i].length;

    if (average >= 90) { var result = "A" }
    else if ( average >= 80 && average < 90 ) { var result = "B" }
    else if ( average >= 70 && average < 80 ) { var result = "C" }
    else { var result = "Failed" }

    console.log(result);
}

Upvotes: 1

Abhishek Sharma
Abhishek Sharma

Reputation: 3300

If you are new at programming or javascript, practice some basic examples first and try to understand how the code should be structured in a way you can manage and reuse it. Basically functional programming at least.

From what I understood from your code, you need something that can dynamically calculate the grades of the students. I have re rewritten the code hope that helps. Also, try to debug the code on your own so as to figure out how the code flows.

var studentGrades = [
  [80, 90, 94],
  [80, 90, 94],
  [80, 90, 94]
]
function evalGrades(grades) {
  var sum = 0;
  for(var i =0; i<grades.length; i++){
    sum = sum + grades[i];
  }
  var avg = sum/grades.length;
  if (avg >= 90) {
    return "A"
  } else if ((avg >= 80) && (avg < 90)) {
    return "B"
  } else if ((avg >= 70) && (avg < 80)) {
    return "C"
  } else {
    return "Failed"
  }
}
for (var i = 0; i < studentGrades.length; i++) {
    console.log(evalGrades(studentGrades[i]))
}


Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386680

You could take an exit early approach fro getting the grade. No else parts are necessary bycause of the return statement.

For getting the average, you could take a dynamic approach with adding values and divide by the length of the array.

const
    add = (a, b) => a + b,
    getAverage = array => array.reduce(add, 0) / array.length,
    evalGrades = grade => {
        if (grade >= 90) return "A";
        if (grade >= 80) return "B";
        if (grade >= 70) return "C";
        return "Failed";
    },
    studentGrades = [[80, 90, 94], [80, 70, 60], [76, 82, 91]],
    studentAvgerages = studentGrades.map(getAverage);
    

console.log(...studentAvgerages);
console.log(...studentAvgerages.map(evalGrades));

Upvotes: 1

Related Questions