This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 280
Expand file tree
/
Copy path2-array-of-objects.js
More file actions
91 lines (83 loc) · 2.72 KB
/
2-array-of-objects.js
File metadata and controls
91 lines (83 loc) · 2.72 KB
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
/*
This exercise includes an array of objects. You can read more about objects here: https://javascript.info/object
Imagine you're working for an online store selling books (like Amazon).
Below, we have an array of book objects.
Each object contains the title of the book, the genre, and a rating based on user reviews.
We want to find the title of the highest rated book in each genre to showcase on our home page.
Implement a function which takes the array of books as a parameter, and returns an array of book titles.
Each title in the resulting array should be the highest rated book in its genre.
*/
// note: Steps to create a function. think about argument. think about input and output and its types. array of object mean array of what type or what type of array.
// blue word before the colon is key or value or property
// we can use teh square bracket
// const books = [{title: "The Lion King"}] // of an array
// const book = {title; "The Lion King"}
// 2 ways to access the value:
// book.title // "The Lion King"
// book["tile"] //"The Lion King"
function getHighestRatedInEachGenre(books) {
// TODO
}
/* ======= Book data - DO NOT MODIFY ===== */
const BOOKS = [
{
title: "The Lion, the Witch and the Wardrobe",
genre: "children",
rating: 4.7
},
{
title: "Sapiens: A Brief History of Humankind",
genre: "non-fiction",
rating: 4.7
},
{
title: "Nadiya's Fast Flavours",
genre: "cooking",
rating: 4.7
},
{
title: "Harry Potter and the Philosopher's Stone",
genre: "children",
rating: 4.8
},
{
title: "A Life on Our Planet",
genre: "non-fiction",
rating: 4.8
},
{
title: "Dishoom: The first ever cookbook from the much-loved Indian restaurant",
genre: "cooking",
rating: 4.85
},
{
title: "Gangsta Granny Strikes Again!",
genre: "children",
rating: 4.9
},
{
title: "Diary of a Wimpy Kid",
genre: "children",
rating: 4.6
},
{
title: "BOSH!: Simple recipes. Unbelievable results. All plants.",
genre: "cooking",
rating: 4.6
},
{
title: "The Book Your Dog Wishes You Would Read",
genre: "non-fiction",
rating: 4.85
},
]
/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the highest rated book in each genre", () => {
expect(new Set(getHighestRatedInEachGenre(BOOKS))).toEqual(new Set(
[
"The Book Your Dog Wishes You Would Read",
"Gangsta Granny Strikes Again!",
"Dishoom: The first ever cookbook from the much-loved Indian restaurant"
]
));
});