forked from MUSA611-CPLN692-spring2020/MUSA611-CPLN692-week4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.js
169 lines (132 loc) · 4.31 KB
/
part1.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* =====================
# Lab 1, Part 1 — More Underscore
## Introduction
Set variables "query1" through "query7" by using underscore functions to answer
the specified question.
There are two stretch goals. In the final one, you will be asked to use a templating
system. You are encouraged to at least try and use underscore's templating function
as it will give you some idea about how an important and common problem in computing
(i.e. how can I create text given a collection of data?)
===================== */
var bakedGoods = [
{
"name": "Carrot",
"type": "Cake",
"inventory": 44,
"price": 3.49
},
{
"name": "Chocolate",
"type": "Cake",
"inventory": 21,
"price": 3.49
},
{
"name": "Sourdough",
"type": "Bread",
"inventory": 5,
"price": 5.29
},
{
"name": "Tiramisu",
"type": "Cake",
"inventory": 15,
"price": 4.99
},
{
"name": "Rye",
"type": "Bread",
"inventory": 6,
"price": 5.09
},
{
"name": "Whole Wheat",
"type": "Bread",
"inventory": 39,
"price": 4.49
},
];
var printMenu = function(foodList) {
_.each(foodList, function(food) {
console.log(food.name + ' ... $' + food.price);
});
};
console.log('List of baked goods', bakedGoods);
/* =====================
Is printMenu a function? Answer this question with underscore. Should evaluate
to true.
===================== */
var query1 = _.isFunction(printMenu);
console.log('printMenu is a function:', query1);
/* =====================
Is bakedGoods an array? Answer this question with underscore. Should evaluate
to true.
===================== */
var query2 = _.isArray(bakedGoods);
console.log('bakedGoods is an array:', query2);
/* =====================
Is the first element in bakedGoods an object? Answer this question with
underscore. Should evaluate to true.
===================== */
var query3 = _.isObject(bakedGoods[1]);
console.log('The first element in bakedGoods is an object:', query3);
/* =====================
Use _.where to return all cakes. Or bread. Whichever is your favorite.
===================== */
var query4 = _.where(bakedGoods, {'type': 'Cake'});
console.log('All bread. Or cakes:', query4);
/* =====================
Use _.filter to return all baked goods that cost more than $4.
===================== */
var query5 = _.filter(bakedGoods, function(x){
return x.price > 4
});
console.log('More than $4:', query5);
/* =====================
Use _.sortBy to order the list by inventory (from lowest to highest).
===================== */
var query6 = _.sortBy(bakedGoods, 'inventory');
console.log('Sorted by inventory (lowest to highest):', query6);
/* =====================
Use _.groupBy to organize the baked goods by type.
===================== */
var query7 = _.groupBy(bakedGoods, 'type');
console.log('Grouped by type:', query7);
/* =====================
Stretch Goal:
Grouping by type (query7) changed the structure of our data. Instead of an array of
objects (e.g. [{}, {}]), we have an object that contains arrays of objects
(e.g. {'key1': [{}, {}], 'key2': [{}, {}]}). Let's do something this structure.
Write a printMenu2 function to receive the new structure (query7) and print
(console.log) a menu with headings. Running printMenu(query7) should log:
Cake
Carrot ... $3.49
Chocolate ... $3.49
Tiramisu ... $4.99
Bread
Sourdough ... $5.29
Rye ... $5.09
Whole Wheat ... $4.49
===================== */
var printMenu2 = function (a){
_.map(a, function(b){
console.log(b[0].type)
_.map(b, function(c){
var list = _.template(`<%= name %> ... $ <%= price %>`);
console.log(list(c))
})
})
};
printMenu2(query7)
/* =====================
Stretch Goal (seriously, this is a bit tough at first!):
We're writing each line of the menu with the code `food.name + " ... $" + food.price`.
While this method technically works, it will become less manageable when the
content becomes more complicated (this can happen as the number of strings and variables
increases). Underscore has a 'templating' system that can be used to clean up this
rendering process.
Use _.template to render the price lines of the menu (Carrot ... $3.49).
Hint: Pay close attention to the example provided in documentation. Copy and paste
it so that you can try it out for yourself. Once you think youunderstand how it
works, give it a try.
===================== */