Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5502
}
160 changes: 131 additions & 29 deletions src/books.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,32 +113,90 @@ 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() {
// Your code here:


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

// Bonus: Iteration 6 | Average Page Count
function averagePageCount() {
// cmd K+U


/* function booksByAuthor_original(bookObj) {
// Your code here:

const newArray = [];
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(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(bookObj)[i];
// const pageAmount = arrayInKey[j].pop();
// const titleName = arrayInKey[j].shift();
const titleName = arrayInKey[j][0];
const pageAmount = arrayInKey[j][1];
newArray.push({
title: titleName,
pages: pageAmount,
author: authorName,
})


}
}
return newArray;
} */





// Bonus: Iteration 6 | Average Page Count
function averagePageCount(bookArr) {
let totalPageCount = 0;
bookArr.forEach((element)=>{
totalPageCount += element.pages;
})
return totalPageCount / bookArr.length;
}
console.log(averagePageCount(booksByAuthor(dictionary)))