Boss lesson · Unit 04
A gradebook
An array of objects turned into an average, a best student, groups and a sorted report
What you practise
- An array of objects: averages, the best student, grouping by grade, a sorted report
Try a question from this lesson
Pick one
A gradebook is an array of objects, one per student. Follow the path: which item, then which key? What does this print?
const students = [
{ name: "Ana", score: 82 },
{ name: "Bo", score: 67 },
{ name: "Cy", score: 91 },
];
console.log(students[1].name);Correct answer
Bo
Why
students[1] is the second object, because indexes start at 0. .name then reads one key from it. An array of objects is the shape most real data comes in: a list of records.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Grouping
To sort things into groups, use an object: each key is a group, and each value is an array of its members.
The first time a group comes up, there is no array under its key yet. Asking for it gives undefined, which is falsy.
So check with !, create an empty array if needed, and then push.
const groups = {};
const grade = "B";
if (!groups[grade]) {
groups[grade] = []; // first time
}
groups[grade].push("Ana");
console.log(groups.B.length); // 111 more exercises in the app
- 4× type the answer
- 1× order the lines
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.