From e8e9dd32051900083d700b99039aa3ea20a240d6 Mon Sep 17 00:00:00 2001 From: Karl97490 Date: Fri, 8 May 2026 19:48:59 +0400 Subject: [PATCH 1/2] first init lab-javascript-book-objects --- src/books.js | 113 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 91 insertions(+), 22 deletions(-) diff --git a/src/books.js b/src/books.js index c8119bb..e39a74d 100644 --- a/src/books.js +++ b/src/books.js @@ -38,14 +38,51 @@ // 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() { - // Your code here: +function getBookDetails(books) { + return `${books.title} - ${books.author} - ${books.pages} pages` } @@ -53,13 +90,22 @@ function getBookDetails() { // Iteration 3 | Delete Language // Your code here: +for (book of booksArray) { + delete book.details.language +} +// console.log(booksArray) // Iteration 4 | Estimated Reading Time // Your code here: +for (book of booksArray) { + const time = Math.ceil((book.pages * 500) / 90) + book.readingTime = time +} +// console.log(booksArray) @@ -69,32 +115,55 @@ function getBookDetails() { The book info is stored in arrays with structure: [title, pages]. */ const dictionary = { - "J. K. Rowling": [ - ["Harry Potter and the Philosopher's Stone", 223], - ["Harry Potter and the Chamber of Secrets", 251], - ["Harry Potter and the Prisoner of Azkaban", 317], - ["Harry Potter and the Goblet of Fire", 636], - ], - "Neal Stephenson": [ - ["Cryptonomicon", 928], - ["Anathem", 1008], - ["Fall; or, Dodge in Hell", 896], - ], - "Malcolm Gladwell": [ - ["Outliers", 320], - ["Blink", 287], - ], + "J. K. Rowling": [ + ["Harry Potter and the Philosopher's Stone", 223], + ["Harry Potter and the Chamber of Secrets", 251], + ["Harry Potter and the Prisoner of Azkaban", 317], + ["Harry Potter and the Goblet of Fire", 636], + ], + "Neal Stephenson": [ + ["Cryptonomicon", 928], + ["Anathem", 1008], + ["Fall; or, Dodge in Hell", 896], + ], + "Malcolm Gladwell": [ + ["Outliers", 320], + ["Blink", 287], + ], }; -function booksByAuthor() { +function booksByAuthor(dictionary) { // Your code here: - + let bookArr = [] // Create the bookArr that will contain the objects + + for (const author in dictionary) { // Iterate through the dictionnary obj using for in loop + const books = dictionary[author] // get the value of the obj properties - return an array + + for (book of books) { // Iterate through this array that contains the books group by author + const bookObj = {} // Create an empty object bookObj that will contain the informations of each book + bookObj["title"] = book[0] // Since the array books contain nested arrays - get the value of thoses nested arrays - the title of the book + bookObj["pages"] = book[1] // The number of pages of the book + bookObj["author"] = author // Get the author name attached to the book - since they are group by author - get the properti name of the dictionnary obj + bookArr.push(bookObj) // Push the new bookObj with properties title, pages and author to the bookArr + + } + + } + return bookArr // Return the bookArr array that contains books objects } +booksByAuthor(dictionary) // Bonus: Iteration 6 | Average Page Count -function averagePageCount() { +function averagePageCount(bookArr) { // Your code here: - + let sumPages = 0; + const numberOfBooks = bookArr.length + for (bookObj of bookArr) { + const bookPages = bookObj.pages + sumPages += bookPages + } + const averagePages = sumPages / numberOfBooks + return averagePages } From 3da4cb154d246850fd5b3f96e86f5124ed9640d8 Mon Sep 17 00:00:00 2001 From: Karl97490 Date: Fri, 8 May 2026 20:02:36 +0400 Subject: [PATCH 2/2] make updates to the lab --- src/books.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/books.js b/src/books.js index e39a74d..0f1f5b2 100644 --- a/src/books.js +++ b/src/books.js @@ -63,7 +63,7 @@ const booksArray = [ author: "Tara Westover", details: { language: "English", - description: "Educated is an account of the struggle for self-invention." + description: "Educated is an account of the struggle for self-invention..." } }, { @@ -81,8 +81,8 @@ const booksArray = [ // Iteration 2 | Book Details -function getBookDetails(books) { - return `${books.title} - ${books.author} - ${books.pages} pages` +function getBookDetails(book) { + return `${book.title} - ${book.author} - ${book.pages} pages` } @@ -90,22 +90,22 @@ function getBookDetails(books) { // Iteration 3 | Delete Language // Your code here: -for (book of booksArray) { +for (const book of booksArray) { delete book.details.language } -// console.log(booksArray) +console.log(booksArray) // Iteration 4 | Estimated Reading Time // Your code here: -for (book of booksArray) { +for (const book of booksArray) { const time = Math.ceil((book.pages * 500) / 90) book.readingTime = time } -// console.log(booksArray) +console.log(booksArray) @@ -137,17 +137,17 @@ function booksByAuthor(dictionary) { let bookArr = [] // Create the bookArr that will contain the objects for (const author in dictionary) { // Iterate through the dictionnary obj using for in loop - const books = dictionary[author] // get the value of the obj properties - return an array + const booksAuthor = dictionary[author] // get the value of the obj properties - return an array - for (book of books) { // Iterate through this array that contains the books group by author - const bookObj = {} // Create an empty object bookObj that will contain the informations of each book - bookObj["title"] = book[0] // Since the array books contain nested arrays - get the value of thoses nested arrays - the title of the book - bookObj["pages"] = book[1] // The number of pages of the book - bookObj["author"] = author // Get the author name attached to the book - since they are group by author - get the properti name of the dictionnary obj - bookArr.push(bookObj) // Push the new bookObj with properties title, pages and author to the bookArr + for (book of booksAuthor) { // Iterate through this array that contains the books group by author + const bookObj = { // Create an empty object bookObj that will contain the informations of each book + title: book[0], // Since the array books contain nested arrays - get the value of thoses nested arrays - the title of the book + pages: book[1], // The number of pages of the book + author: author // Get the author name attached to the book + } + bookArr.push(bookObj) // Push the new bookObj with properties title, pages and author to the bookArr array } - } return bookArr // Return the bookArr array that contains books objects } @@ -158,11 +158,13 @@ booksByAuthor(dictionary) // Bonus: Iteration 6 | Average Page Count function averagePageCount(bookArr) { // Your code here: + if(!bookArr.length){ + return null + } let sumPages = 0; const numberOfBooks = bookArr.length - for (bookObj of bookArr) { - const bookPages = bookObj.pages - sumPages += bookPages + for (const bookObj of bookArr) { + sumPages += bookObj.pages } const averagePages = sumPages / numberOfBooks return averagePages