Repository navigation
Expand file tree
/
Copy pathchallenges.js
More file actions
214 lines (181 loc) · 7.22 KB
/
Copy pathchallenges.js
File metadata and controls
214 lines (181 loc) · 7.22 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
/* eslint-disable */
/* ======================== CallBacks Practice ============================ */
const each = (elements, cb) => {
// Iterates over a list of elements, yielding each in turn to the `cb` function.
// This only needs to work with arrays.
for(let i = 0; i < elements.length; i++) {
cb(elements[i], i);
}
};
const map = (elements, cb) => {
// Produces a new array of values by mapping each value in list through a transformation function.
// Return the new array.
let newArray = [];
for(let i = 0; i < elements.length; i++) {
newArray.push(cb(elements[i]));
}
return newArray;
};
/* ======================== Closure Practice ============================ */
// No test needed here, just run the newCounter(); and make sure it's counting up
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let count = 0;
return () => {
return ++count;
}
// Example: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
};
const newCounter = counter();
console.log(newCounter()); // 1
console.log(newCounter()); // 2
const limitFunctionCallCount = (cb, n) => {
// Should return a function that invokes `cb`.
// The returned function should only allow `cb` to be invoked `n` times.
let count = 0;
return (...args) => {
if (count < n){
count ++;
return cb(...args);
}
return null;
}
};
/* ======================== Prototype Practice ============================ */
// ***Prototypes do NOT have test cases built for them. You must use the console logs provided at the end of this section.***
// Task: You are to build a cuboid maker that can return values for a cuboid's volume or surface area. Cuboids are similar to cubes but do not have even sides.
// Create a CuboidMaker constructor function that accepts properties for length, width, and height
// function CuboidMaker(options) {
// this.length = options.length;
// this.width = options.width;
// this.height = options.height;
// }
// Create a seperate function property of CuboidMaker that returns the volume of a given cuboid's length, width, and height
// Formula for cuboid volume: length * width * height
// CuboidMaker.prototype.volume = function() {
// return this.length * this.width * this.height;
// }
// Create a seperate function property of CuboidMaker that returns the surface area of a given cuboid's length, width, and height.
// Formula for cuboid surface area of a cube: 2(length * width + length * height + width * height)
// CuboidMaker.prototype.surfaceArea = function() {
// return 2 * (this.length * this.width + this.length * this.height + this.width * this.height);
// }
// Formula for cuboid surface area of a cube: 2 * (length * width + length * height + width * height)
// Create a cuboid object that inherits from CuboidMaker.
// The cuboid object must contain keys for length, width, and height.
// const cuboid = new CuboidMaker({
// length: 4,
// width: 5,
// height: 5
// });
// To test your formulas, pass these key/value pairs into your constructor: length: 4, width: 5, and height: 5. When running your logs, you should get Volume: 100 with a Surface Area of 130.
// Use these logs to test your results:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
/* ======================== Class Practice ============================ */
// ***Class Practice does NOT have test cases built. You must use the console logs provided at the end of this section.***
// Task 1: Copy and paste your prototype CuboidMaker here and proceed to convert it into ES6 Class syntax
class CuboidMaker {
constructor(options) {
this.length = options.length;
this.width = options.width;
this.height = options.height;
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea () {
return (
2 *
(this.length * this.width +
this.length * this.height +
this.width * this.height)
);
};
}
// Task 1: Copy and paste your prototype CuboidMaker here and proceed to convert it into ES6 Class syntax. NOTE: Comment our your prototype code above so you don't have conflicting names!
// Task 2: Create a new class called Cube. Extend the Cube class with the CuboidMaker class.
class Cube extends CuboidMaker {
constructor(options) {
super(options);
this.isCube = options.isCube;
}
volume() {
return this.length * this.width * this.height;
}
surfaceArea() {
return (
6 * (this.length * this.width)
);
}
checkIfCube() {
if(this.isCube) {
return 'We have a cube!';
}
}
}
const cuboid = new CuboidMaker({
length: 4,
width: 5,
height: 5,
});
// Create two new methods on the Cube class to calculate the volume and surface area of a cube given the same values passed in from CuboidMaker.
// The volume of a cube is: length * width * height
// The surface area of a cube is: 6 * (length + width)
// Create a new cube object that has equal values for length, width, and height
const cube = new Cube({
length: 2,
width: 2,
height: 2,
isCube: true
});
// To test your formulas, pass these key/value pairs into your constructor: length: 2, width: 2, and height: 2. You should get Volume: 8 with a Surface Area of 24.
// Use these logs to test your results:
console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130
console.log(cube.volume()); // 8
console.log(cube.surfaceArea()); // 24
/* ======================== Stretch Challenges ============================ */
// Challenge 1: Go back to your prototype CuboidMaker and extend Cube using psuedo-classical inheritance to achiveve the same results you built using the ES6 class syntax
// function Cube (options) {
// CuboidMaker.call(this, options)
// }
// Cube.prototype = Object.create(CuboidMaker.prototype);
// Cube.prototype.volume = function () {
// return this.length * this.width * this.height;
// }
// Cube.prototype.surfaceArea = function() {
// return (6 * (this.length * this.width));
// }
// const cube = new Cube({
// length: 2,
// width: 2,
// height: 2,
// });
// Challenge 1: Go back to your prototype CuboidMaker and extend Cube using psuedo-classical inheritance to achiveve the same results you built using the ES6 class syntax. NOTE: Comment our your class code so you don't have conflicting names!
// Use these logs to test your results:
// console.log(cuboid.volume()); // 100
// console.log(cuboid.surfaceArea()); // 130
// console.log(cube.volume()); // 8
// console.log(cube.surfaceArea()); // 24
// Challenge 2: Go back to your class Cube and add the following property: isCube.
// Create a method inside of Cube that checks for isCube and if it's true, returns a string 'We have a cube!'; NOTE: Comment our your prototype code so you don't have conflicting names!
// Use these logs to test your results:
console.log(cuboid.volume()); // 100
console.log(cuboid.surfaceArea()); // 130
console.log(cube.volume()); // 8
console.log(cube.surfaceArea()); // 24
console.log(cube.checkIfCube()); // "We have a cube!"
// Challenge 3: Recursion
const checkMatchingLeaves = obj => {
// return true if every property on `obj` is the same
// otherwise return false
};
module.exports = {
each,
map,
limitFunctionCallCount,
checkMatchingLeaves,
};