-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter4.js
More file actions
116 lines (94 loc) · 2.28 KB
/
Chapter4.js
File metadata and controls
116 lines (94 loc) · 2.28 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
// Exercise 4.1: The Sum of a Range
function range1(start, end, step) {
let arrayOfNums = [];
for(let i = start; i <= end; i++)
arrayOfNums.push(i);
return arrayOfNums;
}
function range2(start, end, step) {
if(step == null)
step = 1;
let arrayOfNums = [];
if(step > 0) {
for(let i = start; i <= end; i+=step)
arrayOfNums.push(i);
} else {
for(let i = start; i >= end; i+=step)
arrayOfNums.push(i);
}
return arrayOfNums;
}
function sum(arrayOfNums) {
let sum = 0;
for(let i = 0; i < arrayOfNums.length; i++) {
sum += arrayOfNums[i];
}
return sum;
}
// Exercise 4.2: Reversing an array
function reverseArray(array) {
let newArray = [];
for(let i = array.length - 1; i >=0; i--)
newArray.push(array[i]);
return newArray;
}
function reverseArrayInPlace(array) {
let loopLimit = Math.floor(array.length / 2);
let lastIndex = array.length - 1;
for(let i = 0; i < loopLimit; i++) {
temp = array[i];
array[i] = array[lastIndex];
array[lastIndex] = temp;
lastIndex--;
}
return array;
}
// Exercise 4.3: A list
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 element = list; element; element = element.rest)
array.push(element.value);
return array;
}
function prepend(value, list) {
return {value, rest:list};
}
function nth(list, number) {
let array = listToArray(list);
return array[number];
}
// Recursive nth function
function nth(list, number) {
if(list == null)
return undefined;
else if(number == 0)
return list.value;
else
return nth(list.rest, number - 1);
}
// Exercise 4.4: Deep comparison
function deepEqual(obj1, obj2) {
if(obj1 === obj2)
return true;
if(obj1 == null || typeof obj1 != "object" ||
obj2 == null || typeof obj2 != "object")
return false;
return compareObjects(obj1, obj2);
}
function compareObjects(obj1, obj2) {
let keysObj1 = Object.keys(obj1);
let keysObj2 = Object.keys(obj2);
if(keysObj1.length != keysObj2.length)
return false;
for(let key of keysObj1) {
if(!keysObj2.includes(key) || !deepEqual(obj1[key], obj2[key]))
return false;
}
return true;
}