forked from bhubr/array-map-filter
-
Notifications
You must be signed in to change notification settings - Fork 284
Expand file tree
/
Copy pathex3.js
More file actions
43 lines (34 loc) · 1.35 KB
/
ex3.js
File metadata and controls
43 lines (34 loc) · 1.35 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
/* Array.prototype.filter - Exercise 3
* Write the CONTENTS of the `filterOnPrice` function
* ATTENTION PLEASE, this time it receives TWO arguments:
1. The first is an array (as usual). It contains OBJECTS, each of which having
two attributes: name and price
2. The second is a MAXIMUM PRICE
* Your goal is to return an array, containing ONLY the products whose price isn't ABOVE
the "maximum price" provided as 2nd argument
SMALL HINT:
* Contrary to the quest examples, and the other exercises, this one is difficult to
solve if you write the function you give to filter OUTSIDE filterOnPrice.
* Your best bet is to directly write an anonymous function inside filter's parentheses
(it may be an arrow function, but it doesn't have to)
Sample arguments, i.e. INPUTS:
1. array of products:
[
{ name: 'iPhone X', price: 1159 },
{ name: 'Xiaomi Mi A2', price: 209 },
{ name: 'Samsung Galaxy Note 9', price: 992 },
{ name: 'Huawei P20', price: 480 },
{ name: 'Huawei P20 Pro', price: 649 }
]
2. maximum price: 500
Expected OUTPUT for these sample arguments:
[
{ name: 'Xiaomi Mi A2', price: 209 },
{ name: 'Huawei P20', price: 480 }
]
*/
function filterOnPrice(products, maxPrice) {
return products.filter(price => price.price <= maxPrice)
}
// DON'T TOUCH THIS!
module.exports = filterOnPrice;