-
Notifications
You must be signed in to change notification settings - Fork 10
/
03-27-19.js
96 lines (75 loc) · 2.69 KB
/
03-27-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
'use strict';
/*
Arrays
Create an array of numbers, 1 through 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.
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.
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.
*/
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const forLoop = (arr) => {
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
}
// forLoop(numbers);
const whileLoop = (arr) => {
let count = 0;
while (count < arr.length) {
console.log(arr[count]);
count++;
}
}
// whileLoop(numbers);
const map = (arr, cb) => {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
newArr.push(cb(arr[i]));
}
return newArr;
}
// map(numbers, (num) => num + 1);//?
const filter = (arr, cb) => {
const filteredArr = [];
for (let i = 0; i < arr.length; i++) {
if (cb(arr[i])) filteredArr.push(arr[i]);
}
return filteredArr;
}
// filter(numbers, (num) => (num % 2)); //?
const reduce = (arr, cb, initAcc) => {
let acc = initAcc;
for (let i = 0; i < arr.length; i++) {
acc = cb(acc, arr[i], i);
}
return acc;
}
// reduce(numbers, (acc, num, idx) => {
// acc += num;
// return acc;
// }, 0) //?
/*
Objects
Begin with the starter code below …
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.
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
Create a state object with keys of people and stuff that contain the people and stuff data.
Do this using object destructuring assignment
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)
Prove that the original people, stuff, and state are unchanged.
*/
const people = ['Kookla','Fran','Ollie'];
const stuff = {
tv: 'huge',
radio: 'old',
toothbrush: 'frayed',
cars: ['Toyota','Mazda']
}
let state = {people: people, stuff: stuff};
let newPeople = ['Odie', ...people, 'Garfield'];
const newStuff = {...stuff, cars: [...stuff.cars, 'car']};
let newState = {};
// newPeople;
// newStuff;
state;
newState;