-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter4.js
More file actions
186 lines (159 loc) · 4.14 KB
/
chapter4.js
File metadata and controls
186 lines (159 loc) · 4.14 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
console.log("Chapter 4 Exercise 1");
function range(start, end, step = 1) {
const completeRange = [];
if (end > start) {
for (let i = start; i <= end; i += step) {
completeRange.push(i);
}
} else {
for (let i = start; i >= end; i -= Math.abs(step)) {
completeRange.push(i);
}
}
return completeRange;
}
function sum(numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total;
}
console.log(range(1, 10));
// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log(range(5, 2, -1));
// → [5, 4, 3, 2]
console.log(sum(range(1, 10)));
// → 55
// Books Solution
// function range(start, end, step = start < end ? 1 : -1) {
// let array = [];
// if (step > 0) {
// for (let i = start; i <= end; i += step) array.push(i);
// } else {
// for (let i = start; i >= end; i += step) array.push(i);
// }
// return array;
// }
// function sum(array) {
// let total = 0;
// for (let value of array) {
// total += value;
// }
// return total;
// }
console.log(" ");
console.log(" ");
console.log(" ");
console.log("Chapter 4 Exercise 2");
function reverseArray(array) {
const newArray = [];
for (let slot of array) {
newArray.unshift(slot);
}
return newArray;
}
function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let first = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = first;
}
return array;
}
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
console.log(" ");
console.log(" ");
console.log(" ");
console.log("Chapter 4 Exercise 3");
function arrayToList(array) {
let list = null;
for (let i = array.length - 1; i >= 0; i--) {
list = {value: array[i], rest: list};
}
return list;
}
function listToArray(list,array = []) {
array.push(list.value);
if (list.rest) return listToArray(list.rest,array)
else return array;
}
function prepend(value, list) {
return {value, rest: list};
}
function nth(list, index) {
if (!list) {
return undefined;
} else if (index > 0) {
return nth(list.rest, index - 1);
} else {
return list.value;
}
}
// Book Solutions
// function arrayToList(array) {
// let list = null;
// for (let i = array.length - 1; i >= 0; i--) {
// list = {value: array[i], rest: list};
// }
// return list;
// }
// function listToArray(list) {
// let array = [];
// for (let node = list; node; node = node.rest) {
// array.push(node.value);
// }
// return array;
// }
// function prepend(value, list) {
// return {value, rest: list};
// }
// function nth(list, n) {
// if (!list) return undefined;
// else if (n == 0) return list.value;
// else return nth(list.rest, n - 1);
// }
console.log(arrayToList([10, 20]));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(listToArray(arrayToList([10, 20, 30])));
// → [10, 20, 30]
console.log(prepend(10, prepend(20, null)));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(nth(arrayToList([10, 20, 30]), 1));
// → 20
console.log(" ");
console.log(" ");
console.log(" ");
console.log("Chapter 4 Exercise 3");
function deepEqual(a, b) {
if (a === b) return true;
if (a == null || b == null || typeof a != "object" || typeof b != "object" ) return false;
if (Object.keys(a).length === Object.keys(b).length) {
const aKeys = Object.keys(a), bKeys = Object.keys(b);
for (let key of aKeys) {
if (bKeys.includes(key) && deepEqual(a[key], b[key])) {
return true;
} else {
return false
}
}
} else {
return false
}
}
// Book Solution
// function deepEqual(a, b) {
// if (a === b) return true;
// if (a == null || typeof a != "object" || b == null || typeof b != "object") return false;
// let keysA = Object.keys(a), keysB = Object.keys(b);
// if (keysA.length != keysB.length) return false;
// for (let key of keysA) {
// if (!keysB.includes(key) || !deepEqual(a[key], b[key])) return false;
// }
// return true;
// }