4. Quiz 4 High-order Functions#
Given the following array,
type Book = {
title: string,
author: string,
year: number,
genres: string[],
};
const books: Book[] = [
{
title: "1984",
author: "George Orwell",
year: 1949,
genres: ["Fiction", "Dystopia"],
},
{
title: "The Great Gatsby",
author: "F. Scott Fitzgerald",
year: 1925,
genres: ["Fiction", "Tragedy"],
},
{
title: "To Kill a Mockingbird",
author: "Harper Lee",
year: 1960,
genres: ["Fiction", "Drama"],
},
{
title: "Brave New World",
author: "Aldous Huxley",
year: 1932,
genres: ["Fiction", "Dystopia"],
},
{
title: "Moby Dick",
author: "Herman Melville",
year: 1851,
genres: ["Fiction", "Adventure"],
},
];
Your task is to implement the following functions using only high-order functions such as
.sort()
,.some()
,.filter()
,.flatMap()
,.map()
, and.reduce()
.Solutions utilizing traditional for-loops or
.forEach()
will not be accepted.
Sort the books by their publication year in descending order (newest first), and log the result using
console.log
.Use Array.some() to check if any book was published after the year 1950, and log the result using
console.log
.Create a new array containing only the books that belong to the “Dystopia” genre, and log the result using
console.log
.Create a flattened list of all genres from the books, and log the result using
console.log
.Create a new array of strings for books that are classified as “Dystopia” and were published after the year 1940. Each string should be in the format:
"The book {title}, published in {year}."
, and log the result usingconsole.log
.Calculate the total number of books that were published before the year 1950, and log the result using
console.log
.
Test your solutions on typescriptlang.org: