Bookworm explainer

Aim of the puzzle: Use the .forEach() array method to update the property of each item in an array.
Walk through of solution: The .forEach() array method takes a function as an argument (also known as a callback function), and iterates through an array using each element as the argument for the callback.

The import statement at the top of the code imports the bookShelf array from additional code so it can be used in the puzzle. Each item in this array is an object containing the properties title, author, and isRead. In this puzzle, we will use .forEach() to run callback functions on the array bookShelf and update the property isRead on each object.

The checkIfRead function will print a message to the console that includes the title and isRead properties. We will use this function with .forEach() to show how the isRead property is updated by our second function, markAsRead(), which will update the property to true.

.forEach() is called 3 times below the function declarations. To complete the puzzle, add .forEach(markAsRead) to the bookShelf variable that is on its own line of code.
Sample code solution:

import { bookShelf } from 'grasshopper.books';

function checkIfRead(book) {
    console.log(book.title + ' has been read: ' + book.isRead);
}
function markAsRead(book) {
    book.isRead = true;
}
bookShelf.forEach(checkIfRead);
bookShelf.forEach(markAsRead);
bookShelf.forEach(checkIfRead);

Javascript Concepts: Array Methods, .forEach(), Imports, Function Declarations
Additional Code (hidden code that runs before the puzzle’s code):

const bookShelf = [{
    title: 'Eloquent JavaScript',
    author: 'Marijn Haverbeke',
    isRead: false
},
{
    title: 'If Hemingway Wrote JavaScript',
    author: 'Angus Croll',
    isRead: false
},
{
    title: 'JavaScript for Kids',
    author: 'Nick Morgan',
    isRead: false
}
];

3 Likes

Hi! Why are there 3 bookShelf variables? Why do you add the argument markAsRead only on the middle one? How come they only affect the last 3 books in the list? Wouldn’t one .forEach go through the whole list and change them?
Tnx.

2 Likes

Hey there, do you mean this part of the code?

bookShelf.forEach(checkIfRead);
bookShelf.forEach(markAsRead);
bookShelf.forEach(checkIfRead);

It’s just one variable, but it’s used 3 times to do different things:

On the 1st line, the .forEach(checkIfRead) is looping through bookShelf and printing out a message that says if each book has been read. This will show that the books have not been read.

On the 2nd line, the .forEach(markAsRead) loops through bookShelf again and marks each book as read.

On the 3rd line, we use .forEach(checkIfRead) one more time to print out a message showing that all of the books have been read.

Hope this helps!
Ben

5 Likes

Hi Zandy,
I will try to answer your questions point wise per each question mark.

  1. There are only 3 objects saved in the grasshopper. books. It can be any number of obejcts. In this problem they saved only 3 objects.
  2. markAsRead is added in the middle to update the property of isRead element as true which was previously false. The first and third arguments just print the objects properties.
  3. They affect the last 3 books only because there are only 3 objects in the bookShelf. The output first 3 results are before updating the isRead property, the next 3 results are after updating the isRead property with the argument markAsRead().

Hope this helps.
Abdulla

3 Likes