In this lesson, you started the Riverside Book Club’s year-end report with one member, Maya. You printed a star line for each of her books and a “which did you prefer?” question for every pair of them, by putting one loop inside another.
Key Points
- Maya’s record holds her name and a list of books, and each book is a record with a title, an author, a genre and a rating from 0 to 5. Two of her books also have a review, which this lesson doesn’t use. Reading that shape first tells you what each loop goes through.
- When you put a loop inside another loop, the inner loop runs all of its passes for each single pass of the outer loop. The two loops don’t take turns, one step each.
- The inner loop’s end can come from the outer loop’s current item. In the star chart, the inner loop runs as many times as the book’s rating, so its body runs 5 + 4 + 3 + 4 = 16 times for Maya’s four books.
- Anything that happens once per outer pass goes in the outer body, outside the inner loop. Create the row’s string at the top of the outer body, so each book starts with no stars, and log the line after the inner loop has finished.
- If you create the string above both loops instead, it never starts over, and the stars pile up from book to book: The Martian shows nine stars and Educated shows sixteen.
- To visit every pair once, start the inner loop at the position right after the outer one (
j = i + 1). That skips a book paired with itself and every pair an earlier pass already asked. Maya’s four books give six questions: three, then two, then one, then none.
Final Code
The first script, chart.js, holds Maya’s record and prints her star chart:
chart.js
const maya = {
name: "Maya",
books: [
{
title: "Kindred",
author: "Octavia E. Butler",
genre: "sci-fi",
rating: 5,
review: "Couldn't put it down.",
},
{ title: "The Martian", author: "Andy Weir", genre: "sci-fi", rating: 4 },
{
title: "Circe",
author: "Madeline Miller",
genre: "fantasy",
rating: 3,
review: "Slow start, lovely ending.",
},
{ title: "Educated", author: "Tara Westover", genre: "memoir", rating: 4 },
],
};
for (const book of maya.books) {
let stars = "";
for (let i = 1; i <= book.rating; i++) {
stars += "*";
}
console.log(`${book.title}: ${stars}`);
}
Run it, and Node prints one line per book:
$ node chart.js
Kindred: *****
The Martian: ****
Circe: ***
Educated: ****
The second script, pairs.js, starts with the same maya record and prints a question for every pair of her books:
pairs.js
const maya = {
name: "Maya",
books: [
{
title: "Kindred",
author: "Octavia E. Butler",
genre: "sci-fi",
rating: 5,
review: "Couldn't put it down.",
},
{ title: "The Martian", author: "Andy Weir", genre: "sci-fi", rating: 4 },
{
title: "Circe",
author: "Madeline Miller",
genre: "fantasy",
rating: 3,
review: "Slow start, lovely ending.",
},
{ title: "Educated", author: "Tara Westover", genre: "memoir", rating: 4 },
],
};
for (let i = 0; i < maya.books.length; i++) {
for (let j = i + 1; j < maya.books.length; j++) {
console.log(`${maya.books[i].title} or ${maya.books[j].title}?`);
}
}
Run it, and Node prints the six questions:
$ node pairs.js
Kindred or The Martian?
Kindred or Circe?
Kindred or Educated?
The Martian or Circe?
The Martian or Educated?
Circe or Educated?
Next, in Loop Over an Object’s Keys and Values, you’ll loop over an object’s properties instead of an array’s elements.