-
Notifications
You must be signed in to change notification settings - Fork 10
/
05-31-19.js
129 lines (92 loc) · 3.54 KB
/
05-31-19.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
// Arrays
// Create an array of numbers, 1 through 10
nums = [1,2,3,4,5,6,7,8,9,10]
// Write a function, called forLoop that takes an array as a parameter, runs the array through a for(...) loop and does a console.log() of each element.
function forLoop(arr) {
for(let i=0; i<arr.length; i++){
console.log(arr[i])
}
}
// Write a function, called whileLoop that takes an array as a parameter, runs the array through a while(...) loop and does a console.log() of each element.
function whileLoop(arr) {
let array = [...arr]
while(array.length) {
console.log(array.shift())
}
}
// Implement .map(), .filter(), .reduce() as regular javascript functions that take in an array as a parameter, but do the same things as the real functions.
function map(arr, cb) {
let output = [];
for(let i=0; i<arr.length;i++){
output.push(cb(arr[i]))
}
return output
}
function filter(arr, cb) {
let output = [];
for(let i=0; i<arr.length;i++){
if(cb(arr[i])) output.push(arr[i])
}
return output
}
function reduce(arr, cb, init) {
for(let i=0; i<arr.length;i++){
init = cb(init, arr[i], i)
}
return init
}
// ---------------------------------------------------- //
console.log('forLoop()');
forLoop(nums);
console.log('------------------\n\n');
console.log('whileLoop()');
whileLoop(nums);
console.log('------------------\n\n');
console.log('map()');
let squares = map(nums, (val=>val * val));
console.log({squares});
console.log('------------------\n\n');
console.log('filter()');
let odds = filter(nums, (val => !!(val % 2)));
console.log({odds});
console.log('------------------\n\n');
console.log('reduce()');
let sum = reduce(nums, (acc,num) => {
acc += num;
return acc;
},0);
console.log({sum});
console.log('------------------\n\n');
// ---------------------------------------------------- //
// Objects
// Begin with the starter code below ...
const people = ['Kookla','Fran','Ollie'];
const stuff = {
tv: 'huge',
radio: 'old',
toothbrush: 'frayed',
cars: ['Toyota','Mazda']
}
// Using spread and destructuring assignment, create a new array called `newPeople', which is a copy of the `people` array, with a person named 'Odie' added to the beginning and one named 'Garfield' added to the end.
let newPeople = ['Odie', ... people, 'Garfield'];
// Using spread and destructuring assignment, create a new object called `newStuff', which is a copy of the `stuff` object, with a new car added to the end of the `cars` array within it
const newStuff = {...stuff, cars: [...stuff.cars, 'Chevy']};
// Create a `state` object with keys of people and stuff that contain the `people` and `stuff` data.
// Do this using object destructuring assignment
let state = {people, stuff};
// Using spread and destructuring assignments, create a new object called `newSate`, repeating the `newPeople` and `newStuff` steps above but directly within the people and stuff nodes of the state object (don't just spread in `newPeople` and `newStuff`)
let newState = {people: ['Odie', ...state.people, 'Garfield'], stuff: {...state.stuff, cars: [...state.stuff.cars, 'Nissan']}};
// Prove that the original people, stuff, and state are unchanged.
// const people = ['Kookla','Fran','Ollie'];
console.log({people});
console.log('------------------\n\n');
console.log({newPeople});
console.log('------------------\n\n');
console.log({stuff});
console.log('------------------\n\n');
console.log({newStuff});
console.log('------------------\n\n');
console.log({state});
console.log('------------------\n\n');
console.log({newState});
console.log('------------------\n\n');