From 7887346a87cfc5ef745b81d677af23b4c41ca3a1 Mon Sep 17 00:00:00 2001 From: Moises Pastran Date: Thu, 28 May 2026 18:12:03 +0200 Subject: [PATCH] Solved lab --- src/books.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/src/books.js b/src/books.js index c8119bb..11624eb 100644 --- a/src/books.js +++ b/src/books.js @@ -38,25 +38,73 @@ // Your code here: -const booksArray = []; +const booksArray = [{ + title: "The Old Man and the Sea", + pages: 128, + author: "Ernest Hemingway", + details: { + language: "English", + description: "One of Hemingway's most famous works, it tells the story of Santiago..." + } + }, + { + title: "The Airbnb Story", + pages: 256, + author: "Leight Gallagher", + details: { + language: "English", + description: "This is the remarkable behind-the-scenes story of the creation and growth of Airbnb..." + } + }, + { + title: "Educated - A Memoir", + pages: 352, + author: "Tara Westover", + details: { + language: "English", + description: "Educated is an account of the struggle for self-invention..." + } + }, + { + title: "The Art of Learning", + pages: 288, + author: "Josh Waitzkin", + details: { + language: "English", + description: "The Art of Learning takes readers through Waitzkin's unique journey to excellence. He explains in clear detail how a well-thought-out, principled approach to learning is what separates success from failure." + } + } +]; // Iteration 2 | Book Details -function getBookDetails() { +function getBookDetails(book) { // Your code here: - + return book.title + " - " + book.author + " - " + book.pages + " pages"; } +console.log(getBookDetails({ + title: "The Art of Learning", + pages: 288, + author: "Josh Waitzkin", + details: { /*...*/ } +})); // Iteration 3 | Delete Language // Your code here: +for (let i = 0; i < booksArray.length; i++) { + delete booksArray[i].details.language; +} +for (let i = 0; i < booksArray.length; i++) { + booksArray[i].readingTime = Math.round(booksArray[i].pages * 500 / 90); +} - +console.log(booksArray); // Iteration 4 | Estimated Reading Time // Your code here: @@ -68,7 +116,7 @@ function getBookDetails() { /* The `dictionary` is an object containing books grouped by author. The book info is stored in arrays with structure: [title, pages]. */ -const dictionary = { +const dictionaryExample = { "J. K. Rowling": [ ["Harry Potter and the Philosopher's Stone", 223], ["Harry Potter and the Chamber of Secrets", 251], @@ -86,15 +134,43 @@ const dictionary = { ], }; -function booksByAuthor() { +function booksByAuthor(dictionary) { // Your code here: - + const result = []; + + // Loop through each author in the dictionary + for (const author in dictionary) { + const books = dictionary[author]; + + // Loop through each book of that author + books.forEach(book => { + const [title, pages] = book; + + result.push({ + title: title, + pages: pages, + author: author + }); + }); + } + + return result; } // Bonus: Iteration 6 | Average Page Count -function averagePageCount() { +function averagePageCount(book) { // Your code here: - + let suma = 0; + let cont = 0; + let avg = 0; + for(let i = 0; i < book.length; i++) { + suma += book[i].pages; + cont++; + } + avg = suma / cont; + return avg; } + +console.log(averagePageCount(booksArray)); \ No newline at end of file