Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done-Aayushi #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
18 changes: 18 additions & 0 deletions counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html>
<head>
<title>Space Clicker</title>
</head>

<body>
<script type="text/javascript">
var clicks = 0;
function onClick() {
clicks += 1 ;
document.getElementById("clicks").innerHTML = clicks;
} ;
</script>
<button type="button" onClick="onClick()">Click me</button>
<p>Clicks: <a id="clicks">0</a></p>

</body>
</html>
103 changes: 103 additions & 0 deletions library.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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: "Pride and Prejudice",
author: "Jane Austen",
year: 1813,
pages: 432,
}
];

// Calculate and return the total number of pages in the library
function getTotalPages() {
const totalPages = library.reduce((total, book) => total + book.pages, 0);
return totalPages;
}

// Return an array containing only the titles of the books in the library
function getBookTitles() {
const bookTitles = library.map(book => book.title);
return bookTitles;
}

// Return an array containing the titles of books published after the given year
function getBooksPublishedAfterYear(year) {
const booksAfterYear = library.filter(book => book.year > year).map(book => book.title);
return booksAfterYear;
}

// Calculate and return the average number of pages across all books in the library
function getAveragePages() {
const totalBooks = library.length;
const totalPages = getTotalPages();
const averagePages = totalPages / totalBooks;
return averagePages;
}

// Return the title of the book with the most pages
function getLongestBook() {
const longestBook = library.reduce((maxBook, book) => (book.pages > maxBook.pages ? book : maxBook));
return longestBook.title;
}

// Return an object with author names as keys and arrays of book titles as values
function getAuthorsAndBooks() {
const authorsAndBooks = library.reduce((result, book) => {
if (!result[book.author]) {
result[book.author] = [];
}
result[book.author].push(book.title);
return result;
}, {});
return authorsAndBooks;
}

// Return an object with author names as keys and total number of pages as values
function getTotalPagesByAuthor() {
const totalPagesByAuthor = library.reduce((result, book) => {
if (!result[book.author]) {
result[book.author] = 0;
}
result[book.author] += book.pages;
return result;
}, {});
return totalPagesByAuthor;
}

// Return an object with author names as keys and titles of the shortest book as values
function getShortestBookByAuthor() {
const shortestBookByAuthor = library.reduce((result, book) => {
if (!result[book.author] || book.pages < library.find(b => b.title === result[book.author]).pages) {
result[book.author] = book.title;
}
return result;
}, {});
return shortestBookByAuthor;
}

// Example usage:
console.log("Total Pages:", getTotalPages());
console.log("Book Titles:", getBookTitles());
console.log("Books Published After 1950:", getBooksPublishedAfterYear(1950));
console.log("Average Pages:", getAveragePages());
console.log("Longest Book:", getLongestBook());
console.log("Authors and Their Books:", getAuthorsAndBooks());
console.log("Total Pages by Author:", getTotalPagesByAuthor());
console.log("Shortest Book by Author:", getShortestBookByAuthor());