Created
May 12, 2022 21:05
-
-
Save maxmatthews/e64dd73ef444185a4424dd09aa076c14 to your computer and use it in GitHub Desktop.
Nested Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const schools = [ | |
{ | |
type: "elementary", | |
teachers: [ | |
{ | |
name: "Mr. Smith", | |
class: "Pirate History", | |
students: [ | |
"Toya Whitebeard", | |
"Snooty Helge", | |
"Cutthroat Sue The Daring", | |
], | |
grades: [94, 99], | |
}, | |
{ | |
name: "Mr. Matthews", | |
class: "Coding for Youngin's", | |
students: ["Abigail Vera", "Estelle Sigmund"], | |
grades: [75, 100], | |
}, | |
], | |
}, | |
{ | |
type: "middle", | |
teachers: [ | |
{ | |
name: "Ms. Johnson", | |
class: "Art", | |
students: ["James Garcia", "Robert Miller"], | |
grades: [90, 100], | |
}, | |
{ | |
name: "Mr. Williams", | |
class: "Science", | |
students: ["John Brown", "Andy Jones"], | |
grades: [60, 90], | |
}, | |
], | |
}, | |
{ | |
type: "high", | |
teachers: [ | |
{ | |
name: "Ms. Thorne", | |
class: "Theoretical Programming", | |
students: ["Taylor Taylor", "Jackson Moore"], | |
grades: [30, 64], | |
}, | |
], | |
}, | |
]; | |
//print out all the school types | |
for (const school of schools) { | |
console.log(school.type); | |
} | |
//print out all the teachers in the middle school | |
for (const teacher of schools[1].teachers) { | |
console.log(teacher); | |
} | |
//print out all the class names along with the type of the school | |
for (const school of schools) { | |
for (const teacher of school.teachers) { | |
console.log(teacher.class); | |
} | |
} | |
//print out all the students names | |
for (const school of schools) { | |
for (const teacher of school.teachers) { | |
for (const student of teacher.students) { | |
console.log(school.type + " - " + student); | |
} | |
} | |
} | |
//print out the grade average for each teacher & school | |
for (const school of schools) { | |
let totalScore = 0; | |
let gradeCount = 0; | |
for (const teacher of school.teachers) { | |
let teacherScore = 0; | |
for (const grade of teacher.grades) { | |
teacherScore += grade; | |
totalScore += grade; | |
gradeCount++; | |
} | |
console.log( | |
"Grade average for " + | |
teacher.name + | |
"'s students is: " + | |
teacherScore / teacher.grades.length | |
); | |
} | |
console.log( | |
"Grade average for " + | |
school.type + | |
" school is: " + | |
totalScore / gradeCount | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment