Skip to content

Instantly share code, notes, and snippets.

@dheavy
Last active January 19, 2016 16:26
Show Gist options
  • Save dheavy/e9ca3f5ba95141851188 to your computer and use it in GitHub Desktop.
Save dheavy/e9ca3f5ba95141851188 to your computer and use it in GitHub Desktop.
// In the exemple, each nested array is a "row", and a set of grades for a student.
const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]],
total = 0,
average = 0.0;
// Columnar approach: get average from student.
for (let row = 0; row < grades.length; row++) {
for (let col = 0; col < grades[row].length; col++) {
total += grades[row][col];
}
average = total / grades[row].length;
console.log(`Student ${parseInt(row + 1)} average: ${average.toFixed(2)}.`);
total = 0;
average = 0.0;
}
// Row-wise approach gives average for each test instead.
for (let col = 0; col = grades.length; col++) {
for (let row = 0; row = grades[col].length; row++) {
total += grades[col][row];
}
average = total / grades[row].length;
console.log(`Test ${parseInt(col + 1)} average: ${average.toFixed(2)}`);
total = 0;
average = 0.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment