forked from am-igdtuw/MENTORSHIP-COHORT1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
185 lines (168 loc) · 6.69 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const library = [
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
year: 1925,
pages: 180
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
pages: 281
},
{
title: "1984",
author: "George Orwell",
year: 1949,
pages: 328
},
{
title: "Prideand Prejudice",
author: "Jane Austen",
year: 1813,
pages: 432
}
];
// Total Number of Pages-> This function uses the "reduce" higher-order function
// It starts with an initial value of 0 and iterates through each book in the library array, adding the pages property of each book to the accumulator.
const getTotalPages = () => library.reduce((total, book) => total + book.pages, 0);
console.log("Total Number of Pages:", getTotalPages());
// List of Book Titles -> For each book in the library array, the "map" function extracts the title property and adds it to the new array.
const getBookTitles = () => library.map(book => book.title);
console.log("List of Book Titles:", getBookTitles());
// Books Published After a Given Year -> This function takes a year parameter and filters the library array using the filter higher-order function.
//The "filter" function checks if the year property of each book is greater than the input year, and only includes those books in the result.
const getBooksPublishedAfterYear = year => library.filter(book => book.year > year).map(book => book.title);
console.log("Books Published After 1950:", getBooksPublishedAfterYear(1950));
// Average Number of Pages -> This function divides the total number of pages (calculated using getTotalPages()) by the number of books in the library.
const getAveragePages = () => getTotalPages() / library.length;
console.log("Average Number of Pages:", getAveragePages());
// Longest Book -> It iterates through each book in the library array, comparing the pages property of each book to the current longest book.
const getLongestBook = () => library.reduce((longestBook, book) => book.pages > longestBook.pages ? book : longestBook);
console.log("Longest Book:", getLongestBook().title);
// Authors and Their Books -> It iterates through each book in the library array and adds the book title to the corresponding author's array in the authorsAndBooks object.
const getAuthorsAndBooks = () => {
const authorsAndBooks = {};
library.forEach(book => {
if (!authorsAndBooks[book.author]) {
authorsAndBooks[book.author] = [];
}
authorsAndBooks[book.author].push(book.title);
});
return authorsAndBooks;
};
console.log("Authors and Their Books:", getAuthorsAndBooks());
// Total Number of Pages by Author -> It iterates through each book in the library array and adds the book's pages to the corresponding author's total pages in the totalPagesByAuthor object.
const getTotalPagesByAuthor = () => {
const totalPagesByAuthor = {};
library.forEach(book => {
if (!totalPagesByAuthor[book.author]) {
totalPagesByAuthor[book.author] = 0;
}
totalPagesByAuthor[book.author] += book.pages;
});
return totalPagesByAuthor;
};
console.log("Total Number of Pages by Author:", getTotalPagesByAuthor());
// Shortest Book by Author -> It iterates through each book in the library array and checks if the current book has fewer pages than the current shortest book for that author.
const getShortestBookByAuthor = () => {
const shortestBookByAuthor = {};
library.forEach(book => {
if (!shortestBookByAuthor[book.author] || book.pages < shortestBookByAuthor[book.author].pages) {
shortestBookByAuthor[book.author] = book;
}
});
return shortestBookByAuthor;
};
console.log("Shortest Book by Author:", getShortestBookByAuthor());
const library1 = [
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
year: 1925,
pages: 180,
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
pages: 281,
},
{
title: "1984",
author: "George Orwell",
year: 1949,
pages: 328,
},
{
title: "Pride and Prejudice",
author: "Jane Austen",
year: 1813,
pages: 432,
},
];
// Function to calculate and return the total number of pages in the library
function getTotalPage(library) {
return library.reduce((total, book) => total + book.pages, 0);
}
// Function to get an array containing only the titles of the books
function getBookTitle(library) {
return library.map((book) => book.title);
}
// Function to get an array of book titles published after a given year
function getBookPublishedAfterYear(library, year) {
return library
.filter((book) => book.year > year)
.map((book) => book.title);
}
// Function to calculate and return the average number of pages
function getAveragePage(library) {
const totalPages = getTotalPages(library);
return totalPages / library.length;
}
// Function to get the title of the book with the most pages
function getLongestbook(library) {
const longestBook = library.reduce((prev, current) =>
prev.pages > current.pages ? prev : current
);
return longestBook.title;
}
// Function to get an object where keys are author names and values are arrays of book titles
function getAuthorsAndBook(library) {
return library.reduce((result, book) => {
if (!result[book.author]) {
result[book.author] = [];
}
result[book.author].push(book.title);
return result;
}, {});
}
// Function to get an object where keys are author names and values are total pages written by each author
function getTotalPageByAuthor(library) {
return library.reduce((result, book) => {
if (!result[book.author]) {
result[book.author] = 0;
}
result[book.author] += book.pages;
return result;
}, {});
}
// Function to get an object where keys are author names and values are titles of shortest books
function getShortestBookbyAuthor(library) {
return library.reduce((result, book) => {
if (!result[book.author] || result[book.author].pages > book.pages) {
result[book.author] = book;
}
return result;
}, {});
}
// Testing the functions
console.log("Total Number of Pages:", getTotalPages(library));
console.log("Book Titles:", getBookTitles(library));
console.log("Books Published After 1900:", getBooksPublishedAfterYear(library, 1900));
console.log("Average Number of Pages:", getAveragePages(library));
console.log("Longest Book:", getLongestBook(library));
console.log("Authors and Their Books:", getAuthorsAndBooks(library));
console.log("Total Number of Pages by Author:", getTotalPagesByAuthor(library));
console.log("Shortest Book by Author:", getShortestBookByAuthor(library));