-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4Chapter.js
executable file
·218 lines (167 loc) · 4.79 KB
/
4Chapter.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// PROBLEM 1
function sum(array) {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}
function span(min, max, increment = 1) {
let array = [];
for (let i = min; i <= max; i += increment) {
array.push(i);
}
return array;
}
// console.log(sum(span(1, 10)));
// PROBLEM 2
function reverseArray(array) {
let reversedArray = [];
for (let i = 0; i < array.length; i++) {
reversedArray.unshift(array[i]);
}
return reversedArray;
}
function reverseArrayInPlace(array) {
for (let i = 0; i < array.length / 2; i++) {
let lastIndex = array.length - 1 - i;
let firstItem = array[i];
let lastItem = array[lastIndex];
array[lastIndex] = firstItem;
array[i] = lastItem;
}
return array;
}
const myArray = [1, 2, 3, 4, 5, 6, 7];
// console.log(reverseArray(myArray));
// console.log(reverseArrayInPlace(myArray));
//PROBLEM 3
const list = {
value: 1,
rest: {
value: 2,
rest: {
value: 3,
rest: {
value: 4,
rest: {
value: 5,
rest: null,
}
}
}
}
};
function arrayToList(array) {
let list = { value: array.length, rest: null };
for (let i = array.length - 1; i > 0; i--) {
list = { value: i, rest: list };
}
return list
}
// console.log(arrayToList([1, 2, 3]));
function listToArray(list) {
let array = [];
const loopThroughList = (list) => {
array.push(list.value);
if (list.rest) {
return loopThroughList(list.rest);
}
};
loopThroughList(list);
return array;
}
// console.log(listToArray({ value: 1, rest: { value: 2, rest: { value: 3, rest: null }}}));
function prepend(element, list) {
return { value: element, rest: list };
}
// console.log(prepend(0, list));
function nth(list, number) {
let counter = number;
const loopThroughList = (list) => {
counter --;
return !counter ? list : loopThroughList(list.rest);
};
return loopThroughList(list);
}
// console.log(nth(list, 3));
//PROBLEM 4
const screen = null;
const number1 = 23;
const number2 = 23;
const string1 = "Vladic";
const string2 = "Kostin";
const array1 = [1, 2, ['a', true], 3, { name: 'Kostin', age: 29 }, 4, [1, 2, ['Vladic']]];
const array2 = [1, 2, ['a', true], 3, { name: 'Kostin', age: 29 }, 4, [1, 2, ['Vladic']]];
const function1 = function() { return 'Hello World' };
const function2 = function() { return 'Hello World' };
const car1 = {
type:"Honda",
owner: {
name: "Vladic Kostin",
gender: "Male",
age: 29
},
model:"Accord",
color:"grey",
previousOwners: ['Bob', 'Sally'],
year: 2012,
};
const car2 = {
type:"Honda",
owner: {
name: "Vladic Kostin",
gender: "Male",
age: 29,
},
model:"Accord",
color:"grey",
previousOwners: ['Bob', 'Sally'],
year: 2012,
};
function deepEquals(value1, value2) {
let isItEqual = true;
function valueAnalysis(value1, value2) {
if (value1 === null || value2 === null) isItEqual = false;
if (!isItEqual) return;
// Checks length of objects and arrays, before looping through them
const objectLength = (object1, object2) => Object.keys(object1).length === Object.keys(object2).length;
const arrayLength = (array1, array2) => array1.length === array2.length;
const Compare = {
objects: (object1, object2) => {
for (let prop in object1) {
if(!object1.hasOwnProperty(prop)) continue;
if (prop in object2) {
valueAnalysis(object1[prop], object2[prop]);
}
else {
return false;
}
}
},
arrays: (array1, array2) => {
for (let i = 0; i < array1.length; i++) {
valueAnalysis(array1[i], array2[i]);
}
},
functions: (function1, function2) => function1.toString() === function2.toString(),
values: (value1, value2) => value1 === value2 ? true : isItEqual = false,
};
function determineType(value1, value2) {
if ((typeof value1 === 'object' && !Array.isArray(value1)) && (typeof value2 === 'object' && !Array.isArray(value2)) && objectLength(value1, value2)) return 'objects';
else if (Array.isArray(value1) && Array.isArray(value2) && arrayLength(value1, value2)) return 'arrays';
else if ((typeof value1 === 'function') && (typeof value2 === 'function')) return 'functions';
else return 'values';
}
return Compare[determineType(value1, value2)](value1, value2);
}
valueAnalysis(value1, value2);
return isItEqual;
}
console.log(deepEquals(array1, array2)); //true
// console.log(deepEquals(screen, car2)); //false
// console.log(deepEquals(number1, number2)); //true
// console.log(deepEquals(string1, string2)); //false
// console.log(deepEquals(array1, array2)); //true
console.log(deepEquals(car1, car2)); //true
// console.log(deepEquals(function1, function2)); //true