forked from beverlyAH/hofPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
144 lines (117 loc) · 3.35 KB
/
practice.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This repo is optional extra practice to use the underscore functions.
// Here we'll be writing new functions, but these functions will use
// the underscore functions within them.
/*
*
* _.each
*
*/
// use _.each to create a copy of the given array.
var moreFruits = function(fruits) {
var results = [];
_.each(fruits, function(fruit, index, collection) {
results.push(fruit);
});
return results;
};
// use _.each to traverse the number array and determine
// which are multiples of five.
var multiplesOfFive = function(numbers) {
var results = [];
_.each(numbers, function(number, index, numbers) {
if (number % 5 === 0) {
results.push(number);
}
});
return results.length;
};
/*
*
* _.filter
*
*/
// use _.filter to return the fruits array with only the desired fruit.
var onlyOneFruit = function(fruits, targetFruit) {
return _.filter(fruits, function(fruit) {
return fruit === targetFruit;
});
};
// use _.filter to return the fruits array with only fruits
// starting with the letter 'P'.
var startsWith = function(fruits, letter) {
return _.filter(fruits, function(fruit) {
return fruit[0] === letter;
});
};
// return a filtered array containing only cookie-type desserts.
var cookiesOnly = function(desserts) {
return _.filter(desserts, function(dessert) {
return dessert['type'] === 'cookie';
});
};
/*
*
* _.reduce
*
*/
// return the total price of all products.
var sumTotal = function(products) {
return _.reduce(products, function(memo, num, index, products) {
return memo + Number(num['price'].substring(1));
}, 0);
};
// return an object consisting of dessert types and how many of each.
// exampleOutput: { dessertType: 3, dessertType2: 1 }
var dessertCategories = function(desserts) {
var dessertTypes = {};
dessertTypes = _.reduce(desserts, function(memo, item) {
var n = item['type'];
dessertTypes[n] = (dessertTypes[n] + 1 || 1);
return dessertTypes;
}, 0);
return dessertTypes;
};
// given an array of movie data objects,return an array containing
// movies that came out between 1990 and 2000.
// TIP: use an array as your accumulator - don't push to an external array!
var ninetiesKid = function(movies) {
};
// return an boolean stating if there exists a movie with a shorter
// runtime than your time limit.
// timeLimit is an integer representing a number of minutes.
var movieNight = function(movies, timeLimit) {
};
/*
*
* _.map
*
*/
// given an array of strings, use _.map to return a new array containing all
// strings converted to uppercase letters.
var upperCaseFruits = function(fruits) {
};
// given an array of dessert objects, return a new array of objects
// that have a new "glutenFree" property, with a boolean value.
// TIP: Items that contain flour are not gluten-free.
var glutenFree = function(desserts) {
};
// use _.map to return an array of items with their sale prices, with a new property
// containing the sale price. round any decimals to 2 places.
//
// having trouble with decimals? check out this article:
// http://adripofjavascript.com/blog/drips/avoiding-problems-with-decimal-math-in-javascript.html
//
/*
example output:
var salePrices = applyCoupon(groceries, 0.20);
[
{
id: 1,
product: 'Olive Oil',
price: '$12.1',
salePrice: '$9.61'
}
];
*/
var applyCoupon = function(groceries, coupon) {
};