-
Notifications
You must be signed in to change notification settings - Fork 1
/
warmup.js
158 lines (123 loc) · 3.82 KB
/
warmup.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
// Arrays
// TODO
// - Create an array of numbers, 1 through 10
var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// TODO
// - Write a function, called forLoop
// - It takes an array as a parameter
// - It runs the array through a for(...) loop
// - It does a console.log() of each element
function forLoop(arr) {
for (var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
console.log("forLoop()");
forLoop(nums);
console.log("------------------\n\n");
// TODO Write a function, called whileLoop
// - It takes an array as a parameter
// - It runs the array through a while(...) loop
// - It does a console.log() of each element
function whileLoop(arr) {
var i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
}
}
console.log("whileLoop()");
whileLoop(nums);
console.log("------------------\n\n");
// Implement the .map() function from scratch
// - Function takes in an array as a parameter
function map(arr, cb) {
var result = [];
for (var i = 0; i < arr.length; i++) {
result.push(cb(arr[i]));
}
return result;
}
console.log("map()");
let squares = map(nums, (val) => val * val);
console.log({ squares });
console.log("------------------\n\n");
// Implement the .filter() function from scratch
// - Function takes in an array as a parameter
function filter(arr, cb) {
var result = [];
for (var i = 0; i < arr.length; i++) {
if (cb(arr[i])) result.push(arr[i]);
}
return result;
}
console.log("filter()");
let odds = filter(nums, (val) => !!(val % 2));
console.log({ odds });
console.log("------------------\n\n");
// Implement the .reduce() function from scratch
// - Function takes in an array as a parameter
function reduce(arr, cb, init) {
for (var i = 0; i < arr.length; i++) {
init = cb(init, arr[i], i);
}
return init;
}
console.log("reduce()");
let sum = reduce(
nums,
(acc, num) => {
acc += num;
return acc;
},
0,
);
console.log({ sum });
console.log("------------------\n\n");
// Objects
// Characters
const characters = ["Pepe", "Brian", "Roger"];
console.log({ characters });
console.log("------------------\n\n");
// Things
const things = {
instrument: "marimba",
stage: "wooden",
aeroplane: "prop",
cars: ["Ford", "Toyota", "GM"],
};
console.log({ things });
console.log("------------------\n\n");
// TODO
// - Using spread and destructuring assignment
// - Create a new array called `newcharacters', which is a copy of the `characters` array
// - Add a character named 'Pepe' to the beginning
// - Add a character named 'Garfield' added to the end
let newCharacters = ["Pepe", ...characters, "Garfield"];
console.log({ newCharacters });
console.log("------------------\n\n");
// TODO
// Using spread and destructuring assignment
// - Create a new object called `newThing', which is a copy of the `things` object
// - Add a new car added to the end of the `cars` array within the 'things' object
const newThing = { ...things, cars: [...things.cars, "Rolls"] };
console.log({ newThing });
console.log("------------------\n\n");
// TODO
// - Create a `state` object with keys of characters and things that contain the `characters` and `things` data
// - Do this using object destructuring assignment
let state = { characters, things };
console.log({ state });
console.log("------------------\n\n");
// TODO
// - Using spread and destructuring assignments, create a new object called `newSate`
// - Repeating the `newcharacters` and `newThing` steps above but directly within the characters and things nodes of the state object
// * Do not spread in `newcharacters` and `newThing`
let newState = {
...state,
characters: ["Pepe", ...state.characters, "Garfield"],
things: { ...state.things, cars: [...state.things.cars, "Rolls"] },
};
console.log({ newState });
console.log("------------------\n\n");
// Prove that the original characters, things, and state are unchanged.