DEV Community

Discussion on: Jagged Array in JavaScript

Collapse
 
aminnairi profile image
Amin

Interesting, I didn't even know it had a name.

Thanks for sharing!

I think that by using a forEach loop, you can decrease the need for using intermediate variables and ease the reading of the algorithm.

"use strict";

const students = [
    [ 18 ],
    [ 16, 12, 8 ],
    [ 2, 12 ]
];

students.forEach((grades, index) => {
    const rank = index + 1;
    const total = grades.reduce((sum, grade) => sum + grade, 0);
    const average = total / grades.length;

    console.log(`Student ${rank} average is ${average}.`);
});

// Student 1 average is 18.
// Student 2 average is 12.
// Student 3 average is 7.

What do you think?

Collapse
 
osejudith profile image
Judith ☁ Oiku

Am glad you found it resourceful, Yeah, you are right, thanks so much for sharing