-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
230 lines (221 loc) · 6.46 KB
/
Copy pathindex.js
File metadata and controls
230 lines (221 loc) · 6.46 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @typedef {{cohort: number, name: string}} Student
* @param {Student} student
* @returns {number} the cohort of the student
*
* @example
* getCohort({cohort: 2012, name: "Alice"}); // 2012
* @example
* getCohort({cohort: 1937, name: "Bob"}); // 1937
* @example
* getCohort({cohort: 3476, name: "Charlie"}); // 3476
*/
export function getCohort(student) {
return student["cohort"];
}
/**
* @typedef {{cohort: number, name: string}} Student
* Hint: comparison operators (e.g. <, <=, >, >=) work on strings!
* @param {Student} studentA
* @param {Student} studentB
* @returns {Student} the student whose name comes first alphabetically
* @returns {Student} studentA if both students have the same name
*
* @example
* sortStudents({cohort: 1, name: "Alice"}, {cohort: 1, name: "Bob"}); // {cohort: 1, name: "Alice"}
* @example
* sortStudents({cohort: 1, name: "Bob"}, {cohort: 1, name: "Alice"}); // {cohort: 1, name: "Alice"}
* @example
* sortStudents({cohort: 1, name: "Alice"}, {cohort: 2, name: "Alice"}); // {cohort: 1, name: "Alice"}
*/
/*I can't figure how to make the caps sensitive test pass. I will try later if I can. But I have already spent too much time on this so I'm going to focus on something else for now*/
export function sortStudents(studentA, studentB) {
const names = [studentA.name, studentB.name];
if (studentA.name <= studentB.name) {
return studentA;
} else if (studentB.name < studentA.name) {
return studentB;
} else {
return "";
}
}
/**
* @typedef {{color: string, icon: string}} Flag
* @param {string} color
* @param {string} icon
* @returns {Flag} an object with the given color and icon
*
* @example
* makeFlag("red", "circle"); // { color: "red", icon: "circle" }
* @example
* makeFlag("blue", "square"); // { color: "blue", icon: "square" }
* @example
* makeFlag("yellow", "triangle"); // { color: "yellow", icon: "triangle" }
*/
export function makeFlag(color, icon) {
let finObject = {};
finObject.color = color;
finObject.icon = icon;
return finObject;
}
/**
* @typedef {{value: number}} Count
* @param {Count} count
* @returns {Count} a count whose value is 1 more than the given count
*
* @example
* increment({value: 1}); // {value: 2}
* @example
* increment({value: 0}); // {value: 1}
* @example
* increment({value: -5}); // {value: -4}
*/
export function increment(count) {
count.value += 1;
return count;
}
/**
* @typedef {{x: number, y: number}} Location
*
* In a city with a rectangular grid of streets, a taxicab can only
* travel along the grid lines. The taxicab distance between two locations
* is the sum of the absolute differences of their x and y coordinates.
*
* You may assume that the coordinates will be integers.
*
* See: https://en.wikipedia.org/wiki/Taxicab_geometry
*
* @param {Location} from
* @param {Location} to
* @returns {number} the taxicab distance between the two locations
*
* @example
* getTaxicabDistance({x: 0, y: 0}, {x: 3, y: 4}); // 7
* @example
* getTaxicabDistance({x: -1, y: -2}, {x: 1, y: 2}); // 6
* @example
* getTaxicabDistance({x: 5, y: 5}, {x: 5, y: 5}); // 0
*
*/
export function getTaxicabDistance(from, to) {
let dist1 = Math.abs(from.x - to.x);
let dist2 = Math.abs(from.y - to.y);
return dist1 + dist2;
}
/**
* @typedef {{name: string, isHerbivore: boolean}} Animal
* @param {Animal[]} animals
* @returns {Animal[]} an array of only the herbivorous animals
*
* @example
* getHerbivores([{name: "Cow", isHerbivore: true}, {name: "Lion", isHerbivore: false}]); // [{name: "Cow", isHerbivore: true}]
* @example
* getHerbivores([]); // []
* @example
* getHerbivores([{name: "Rabbit", isHerbivore: true}]); // [{name: "Rabbit", isHerbivore: true}]
*/
export function getHerbivores(animals) {
let finObject = [];
for (let i = 0; i < animals.length; i++) {
if (animals[i].isHerbivore === true) {
finObject[i] = animals[i];
}
}
return finObject;
}
/**
* @typedef {{name: string, isCarnivore: boolean}} Animal
* @param {Animal[]} animals
* @returns {string[]} an array of the names of the carnivorous animals
*
* @example
* getCarnivoreNames([{name: "Lion", isCarnivore: true}, {name: "Cow", isCarnivore: false}]); // ["Lion"]
* @example
* getCarnivoreNames([]); // []
* @example
* getCarnivoreNames([{name: "Wolf", isCarnivore: true}]); // ["Wolf"]
*/
export function getCarnivoreNames(animals) {
let finName = [];
for (let i = 0; i < animals.length; i++) {
if (animals[i].isCarnivore) {
finName.push(animals[i].name);
}
}
return finName;
}
/**
* @typedef {{name: string, quantity: number, price: number}} Item
*
* You may assume that:
* - quantities will be non-negative integers
* - prices will be non-negative numbers
*
* @param {Item[]} cart
* @returns {number} the total cost of all items in the given cart
*
* @example
* getTotalCost([{name: "Hourglass", quantity: 2, price: 3.12}, {name: "Comb", quantity: 1, price: 2.50}]); // 8.74
* @example
* getTotalCost([]); // 0
* @example
* getTotalCost([{name: "Notebook", quantity: 0, price: 5}]); // 0
*/
export function getTotalCost(cart) {
let cartTotal = 0;
for (let i = 0; i < cart.length; i++) {
if (cart[i].quantity > 0) {
cartTotal += cart[i].price * cart[i].quantity;
}
}
return cartTotal;
}
/**
* Zip is an operation that merges two arrays into a single object.
* You can assume that the two input arrays have the same length.
*
* @param {string[]} keys
* @param {T[]} values
* @returns {{[key: string]: T}} an object where each key is associated with the
* corresponding value at the same index
*
* @example
* zip(["a", "b"], [1, 2]); // {a: 1, b: 2}
* @example
* zip([], []); // {}
* @example
* zip(["x"], ["x"]); // {x: "x"}
*/
export function zip(keys, values) {
let zipped = {};
for (let i = 0; i < zip.length; i++) {
let key = keys[i];
let value = values[i];
zipped[key] = value;
}
return zipped;
}
/**
* @param {string} word
* @returns {{[character: string]: number}} an object where each key is a character
* and its value is the number of times that character appears in the given word
*
* @example
* countCharacters("hello"); // {h: 1, e: 1, l: 2, o: 1}
* @example
* countCharacters(""); // {}
* @example
* countCharacters("aAa"); // {a: 2, A: 1}
*/
export function countCharacters(word) {
const count = {};
for (let i = 0; i < word.length; i++) {
let char = word[i];
if (!(char in count)) {
count[char] = 1;
} else {
count[char]++;
}
}
return count;
}