Tell us what’s happening:
Your getTotalPages function should return the total number of pages in the books from the array passed to it. i have done a lot of things and the output is correct but it is still showing me thi error
Your code so far
// User Editable Region
// Sample data with a 'pages' property for each book
const library = [
{
title: "The Hitchhiker's Guide to the Galaxy",
author: "Douglas Adams",
about: "A comedic sci-fi adventure.",
pages: 224,
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
about: "A story about racial injustice in the American South.",
pages: 336,
},
{
title: "Zero to Sold",
author: "Arvid Kahl",
about: "A book on how to bootstrap a business.",
pages: 204,
},
{
title: "1984",
author: "George Orwell",
about: "A dystopian social science fiction novel.",
pages: 328,
},
{
title: "The Embedded Entrepreneur",
author: "Arvid Kahl",
about: "A book focusing on how to build an audience-driven business.",
pages: 280,
},
{
title: "Atomic Habits",
author: "James Clear",
about: "A practical book about discarding bad habits and building good ones.",
pages: 320,
},
];
/**
* Calculates the total number of pages for all books in an array using reduce().
* @param {Array<Object>} books - An array of book objects, where each has a 'pages' property.
* @returns {number} The total page count.
*/
const getTotalPages = (books) => {
// Use the reduce() method to sum the 'pages' property of each book.
// 'acc' is the accumulator, which holds the running total.
// 'book' is the current book object.
// The initial value of the accumulator is 0.
return books.reduce((acc, book) => acc + book.pages, 0);
};
// Log the initial message
console.log("\nTotal number of pages for all library books:\n");
// Call the getTotalPages function and log the result to the console
console.log(getTotalPages(library));
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Build a Library Manager - Step 18