count how many books on misa’s bookshelf in javascript

Assuming that Misa's bookshelf is an array of objects in the format {title: "Book Title", author: "Author Name", ...}), you can count how many books are on her bookshelf using the Array.prototype.reduce() method:

index.tsx
const misasBookshelf = [
  {title: "Book 1", author: "Author A"},
  {title: "Book 2", author: "Author B"},
  {title: "Book 3", author: "Author C"},
  // more books...
];

const bookCount = misasBookshelf.reduce((count, book) => count + 1, 0);

console.log(`Misa has ${bookCount} books on her bookshelf.`);
306 chars
11 lines

In the above code, reduce() iterates over each book in misasBookshelf and adds 1 to the count variable for each book, starting from an initial value of 0.

The resulting bookCount variable contains the total number of books on Misa's bookshelf, which is then output to the console using a template literal.

gistlibby LogSnag