From 7889c978508391ece66f0d4867bf86920a54ed4d Mon Sep 17 00:00:00 2001 From: yemi Date: Fri, 8 May 2026 16:55:41 +0100 Subject: [PATCH 1/4] solved except bonus --- src/books.js | 107 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 28 deletions(-) diff --git a/src/books.js b/src/books.js index c8119bb..ba1d15e 100644 --- a/src/books.js +++ b/src/books.js @@ -38,29 +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' + } +}, { + 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` } // Iteration 3 | Delete Language // Your code here: - - - +function deleteLanguage(bookArr){ + bookArr.forEach(function(element,index){ + delete element.details.language; + }) + return bookArr +} +deleteLanguage(booksArray); // Iteration 4 | Estimated Reading Time // Your code here: - - +function estReadingTime(bookArr){ + let timeInSeconds = 0; + bookArr.forEach(function(element){ + timeInSeconds = Math.ceil((element.pages * 500)/90); + element.readingTime = timeInSeconds; + }) + return bookArr; +} +estReadingTime(booksArray); // Bonus: Iteration 5 | Books Dictionary @@ -69,32 +113,39 @@ 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(dictio) { // Your code here: - + const newArray = [{}]; + for (let i = 0; i < Object.entries(dictio).length; i++) { + for (let j = 0; j < Object.values(dictio)[i].length; j++) { + console.log(Object.values(dictio)[i][j]); + } + } //maybe a nested loop for keys and another for values } - +console.log(booksByAuthor(dictionary)); // Bonus: Iteration 6 | Average Page Count function averagePageCount() { // Your code here: - } + + +// console.log(Object.keys(dictionary)) +// console.log(Object.values(dictionary)[0]) \ No newline at end of file From 29de43535cf6c7fbb21cc04daf84be5e91d189c6 Mon Sep 17 00:00:00 2001 From: yemi Date: Fri, 8 May 2026 20:18:21 +0100 Subject: [PATCH 2/4] bonus 1 done --- src/books.js | 55 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/src/books.js b/src/books.js index ba1d15e..879fb37 100644 --- a/src/books.js +++ b/src/books.js @@ -86,8 +86,8 @@ function getBookDetails(book) { // Iteration 3 | Delete Language // Your code here: -function deleteLanguage(bookArr){ - bookArr.forEach(function(element,index){ +function deleteLanguage(bookArr) { + bookArr.forEach(function (element, index) { delete element.details.language; }) return bookArr @@ -96,10 +96,10 @@ deleteLanguage(booksArray); // Iteration 4 | Estimated Reading Time // Your code here: -function estReadingTime(bookArr){ +function estReadingTime(bookArr) { let timeInSeconds = 0; - bookArr.forEach(function(element){ - timeInSeconds = Math.ceil((element.pages * 500)/90); + bookArr.forEach(function (element) { + timeInSeconds = Math.ceil((element.pages * 500) / 90); element.readingTime = timeInSeconds; }) return bookArr; @@ -129,18 +129,51 @@ const dictionary = { ['Blink', 287], ], }; -function booksByAuthor(dictio) { + +function booksByAuthor(bookArr) { // Your code here: - const newArray = [{}]; - for (let i = 0; i < Object.entries(dictio).length; i++) { - for (let j = 0; j < Object.values(dictio)[i].length; j++) { - console.log(Object.values(dictio)[i][j]); + const newArray = []; + const entiresAmount = Object.entries(bookArr).length;//3 entries + let key = ''; // capture changing key name per iteration, so we can enter each array + for(let i=0; i Date: Fri, 8 May 2026 21:45:12 +0100 Subject: [PATCH 3/4] solved except missing one check --- src/books.js | 53 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 10 deletions(-) diff --git a/src/books.js b/src/books.js index 879fb37..c39ea13 100644 --- a/src/books.js +++ b/src/books.js @@ -135,15 +135,17 @@ function booksByAuthor(bookArr) { const newArray = []; const entiresAmount = Object.entries(bookArr).length;//3 entries let key = ''; // capture changing key name per iteration, so we can enter each array - for(let i=0; i Date: Wed, 13 May 2026 17:32:32 +0100 Subject: [PATCH 4/4] solved full lab --- .vscode/settings.json | 3 ++ src/books.js | 93 ++++++++++++++++++------------------------- 2 files changed, 42 insertions(+), 54 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f673a71 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file diff --git a/src/books.js b/src/books.js index c39ea13..129fc29 100644 --- a/src/books.js +++ b/src/books.js @@ -130,22 +130,47 @@ const dictionary = { ], }; -function booksByAuthor(bookArr) { + +function booksByAuthor(authorsObj) { + const booksArr = [] + + for (const authorName in authorsObj) { + authorsObj[authorName].forEach(function (bookDataArr) { + const newBook = { + title: bookDataArr[0], + pages: bookDataArr[1], + author: authorName, + } + booksArr.push(newBook) + }) + } + return booksArr +} + +const sortedDictionary = booksByAuthor(dictionary); + +// for of --> iterables (arr, str) +// for in --> objects + +// cmd K+U + + +/* function booksByAuthor_original(bookObj) { // Your code here: const newArray = []; - const entiresAmount = Object.entries(bookArr).length;//3 entries + const entiresAmount = Object.entries(bookObj).length;//3 entries let key = ''; // capture changing key name per iteration, so we can enter each array for (let i = 0; i < entiresAmount; i++) { - key = Object.keys(bookArr)[i]; - arrayInKey = bookArr[key]; + key = Object.keys(bookObj)[i]; + arrayInKey = bookObj[key]; for (let j = 0; j < arrayInKey.length; j++) { // console.log(Object.keys(bookArr)[i]) //author names // console.log(arrayInKey[j]); // title and pages // console.log(arrayInKey[j][0]); // title // console.log(arrayInKey[j][1]); // pages - const authorName = Object.keys(bookArr)[i]; + const authorName = Object.keys(bookObj)[i]; // const pageAmount = arrayInKey[j].pop(); - // const titleName = arrayInKey[j].shift() + // const titleName = arrayInKey[j].shift(); const titleName = arrayInKey[j][0]; const pageAmount = arrayInKey[j][1]; newArray.push({ @@ -158,60 +183,20 @@ function booksByAuthor(bookArr) { } } return newArray; -} +} */ + -console.log(booksByAuthor(dictionary)); // Bonus: Iteration 6 | Average Page Count function averagePageCount(bookArr) { - // Your code here: - let bookCount = 0; - let pageAmount = 0; - const entiresAmount = Object.entries(bookArr).length;//3 entries - let key = ''; // capture changing key name per iteration, so we can enter each array - for (let i = 0; i < entiresAmount; i++) { - key = Object.keys(bookArr)[i]; - const arrayInKey = bookArr[key]; - bookCount += arrayInKey.length - for (let j = 0; j < arrayInKey.length; j++) { - - pageAmount += arrayInKey[j][1]; - - } - // console.log(average) - // console.log(arrayInKey.length) - - } - const averagePages = pageAmount/bookCount; - return averagePages; + let totalPageCount = 0; + bookArr.forEach((element)=>{ + totalPageCount += element.pages; + }) + return totalPageCount / bookArr.length; } - -console.log(averagePageCount(dictionary)); - - - - - - - - - - - - - - - - - - - - - - - - +console.log(averagePageCount(booksByAuthor(dictionary)))