-
Notifications
You must be signed in to change notification settings - Fork 0
/
83 A list.js
119 lines (101 loc) · 2.85 KB
/
83 A list.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
'use strict';
/* BOOK SOLUTION
function arrayToList(array) {
let list = null;
for (let i = array.length - 1; i >= 0; i--) {
list = {value: array[i], rest: list}; // <-- BETTER SOLUTION: NEST THE OWN OBJECT DECLARING A NEW ONE
}
return list;
}
function listToArray(list) {
let array = [];
for (let node = list; node; node = node.rest) { // <-- AWESOME! USE THE FOR TO ITERATE OVER THE LIST IN A VERY BETTER WAY
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 */
function arrayToList (array) {
let list = {};
let rest = null;
for (let i = array.length - 1; i >= 0; i--) {
list.value = array[i];
list.rest = rest;
rest = {};
rest.value = list.value;
rest.rest = list.rest;
}
return list;
}
function listToArray (list) {
const array = [];
function recursiveListAccess(data) { // Solution using Closure, accessing the outer function binding to store values. SEE BOOK SOLUTION
array.push(data.value);
if (data.rest === null) {
return;
} else {
recursiveListAccess(data.rest);
}
}
recursiveListAccess(list);
return array;
}
function prepend (element, list) {
return {value: element, rest: list};
}
function nth (list, number) {
let nthElement = undefined;
let nthRest = list;
while (number >= 0) {
nthElement = nthRest;
if (nthElement !== null) {
nthRest = nthElement.rest;
}
number--;
}
if (nthElement === null) {
return undefined;
}
return nthElement.value;
}
function recursiveNth (list, number = 0) {
if (!list) return undefined;
else if (number === 0) return list.value;
else return recursiveNth (list.rest, number - 1);
}
let label = '';
let arraySample = [1, 2, 3];
let listTest = arrayToList(arraySample);
console.log('arrayToList:', listTest);
console.log('listToArray test:', listToArray(listTest));
console.log('prepend function:', prepend(0, listTest));
label = 'nth function test:';
console.group(label);
console.log(nth(listTest, 0));
console.log(nth(listTest, 1));
console.log(nth(listTest, 2));
console.log(nth(listTest, 3));
console.log(nth(listTest, 4));
console.groupEnd(label);
label = 'recursiveNth function test:';
console.log(recursiveNth(listTest, 0));
console.log(recursiveNth(listTest, 1));
console.log(recursiveNth(listTest, 2));
console.log(recursiveNth(listTest, 3));
console.log(recursiveNth(listTest, 4));
console.groupEnd(label);