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

chore: add JS only higher order functions lab to frontend cert #586

Open
wants to merge 1 commit 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
48 changes: 48 additions & 0 deletions frontend-cert/js-projects/higher-order-functions/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function sortByYear(book1, book2) {
if (book1.release_year < book2.release_year) {
return -1;
}

if (book1.release_year < book2.release_year) {
return 1;
}

return 0;
}

const books = [
{
title: "The Great Gatsby",
author_name: "F. Scott Fitzgerald",
release_year: 1925,
},
{
title: "In Search of Lost Time",
author_name: "Marcel Proust",
release_year: 1913,
},
{
title: "Ulysses",
author_name: "James Joyce",
release_year: 1922,
},
{
title: "One Hundred Years of Solitude",
author_name: "Gabriel García Márquez",
release_year: 1967,
},
{
title: "The Catcher in the Rye",
author_name: "J. D. Salinger",
release_year: 1951,
},
];

let filteredBooks = books.filter((book) => book.release_year < 1950);

let filteredBooksSorted = filteredBooks.sort(sortByYear);

console.log("Books Written Before 1950 (sorted according to release year)");
filteredBooksSorted.forEach((book) => {
console.log(`${book.title} by ${book.author_name} (${book.release_year})`);
});
15 changes: 15 additions & 0 deletions frontend-cert/js-projects/higher-order-functions/user-stories.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
You should have an array of objects named `books` where each object in the array should have a string `title`, another string `author_name`, and a number `release_year`.

You should call the filter higher-order function on the `books` array with a callback to filter out books written after a certain year such as 1950 and save the filtered array in a new array named `filteredBooks`.

You should call the `sort` higher order function on the `filteredBooks` array to filter the books according to their `release_year` in ascending order and save the filtered array in a new array named `filteredBooksSorted`.

You should have a callback function named `sortByYear` that accepts two books, `book1`, and `book2` as parameter for sorting the array.

The `sortByYear` function should return `-1` if the `release_year` of `book1` object is smaller than that of the `book2` object.

The `sortByYear` function should return `1` if the `release_year` of `book1` object is bigger than that of the `book2` object.

The `sortByYear` function should return `0` for all other scenarios.

You should call the `forEach` higher order function on the `filteredBooksSorted` array and print out the details of each book following the `<BOOK_TITLE> by <BOOK_AUTHOR> (<RELEASE_YEAR>)` format.