Last active
July 17, 2022 10:57
-
-
Save jonurry/9ceb8e580072fdd4d9d58bb2a9edc5da to your computer and use it in GitHub Desktop.
4.3 A List (Eloquent JavaScript Solutions)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayToList(array) { | |
let result = {}; | |
if (Array.isArray(array)) { | |
let currListItem = result; | |
for (let item of array) { | |
let newListItem = { | |
value: item, | |
rest: null | |
}; | |
if (typeof currListItem.rest === 'undefined') { | |
result = newListItem; | |
} else { | |
currListItem.rest = newListItem; | |
} | |
currListItem = newListItem; | |
} | |
} | |
return result; | |
} | |
function listToArray(list) { | |
let result = []; | |
if (typeof list === 'undefined' || list.value === undefined || list.rest === undefined) { | |
return result; | |
} else { | |
result.push(list.value); | |
while (list.hasOwnProperty('rest') && list.rest !== null) { | |
list = list.rest; | |
if (list.hasOwnProperty('value')) { | |
result.push(list.value); | |
} | |
} | |
} | |
return result; | |
} | |
function prepend(element, list) { | |
return { | |
value: element, | |
rest: list | |
}; | |
} | |
function nth(list, number) { | |
return listToArray(list)[number]; | |
} | |
function nthRecursive(list, number) { | |
if (number === 0) { | |
return list.value; | |
} else if (list.rest === null) { | |
return undefined; | |
} else { | |
return nthRecursive(list.rest, number-1); | |
} | |
} | |
console.log(arrayToList()); | |
console.log(arrayToList([10, 20])); | |
// → {value: 10, rest: {value: 20, rest: null}} | |
console.log(arrayToList([4, 3, 2, 1])); | |
// → {value: 4, rest: {value: 3, rest: {value: 2, rest: {value: 1, rest: null}}}} | |
console.log(listToArray()); | |
// → [] | |
console.log(listToArray(arrayToList([10, 20, 30]))); | |
// → [10, 20, 30] | |
console.log(listToArray({value: 10, rest: {xxx: 20, yyy: null}})); | |
// → [10] | |
console.log(listToArray({value: 10, rest: {value: 20, yyy: null}})); | |
// → [10, 20] | |
console.log(listToArray({value: 10, rest: {xxx: 20, rest: null}})); | |
// → [10] | |
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(nth(arrayToList([10, 20, 30]), 3)); | |
// → undefined | |
console.log(nth(arrayToList([10, 20, 30]), -2)); | |
// → undefined | |
console.log(nthRecursive(arrayToList([10, 20, 30]), 1)); | |
// → 20 | |
console.log(nthRecursive(arrayToList([10, 20, 30]), 3)); | |
// → undefined | |
console.log(nthRecursive(arrayToList([10, 20, 30]), -2)); | |
// → undefined | |
// now with the help of the hints... | |
function arrayToListWithHints(array) { | |
let result = {}; | |
if (Array.isArray(array)) { | |
let list = null; | |
array = array.reverse(); | |
for (let item of array) { | |
list = { | |
value: item, | |
rest: list | |
}; | |
} | |
result = list; | |
} | |
return result; | |
} | |
function listToArrayWithHints(list) { | |
let result = []; | |
if (typeof list === 'undefined' || list.value === undefined || list.rest === undefined) { | |
return result; | |
} else { | |
for (let node = list; node; node = node.rest) { | |
if (node.hasOwnProperty('value')) { | |
result.push(node.value); | |
} | |
} | |
} | |
return result; | |
} | |
console.log('\nWith Hints:'); | |
console.log(arrayToListWithHints()); | |
console.log(arrayToListWithHints([10, 20])); | |
// → {value: 10, rest: {value: 20, rest: null}} | |
console.log(arrayToListWithHints([4, 3, 2, 1])); | |
// → {value: 4, rest: {value: 3, rest: {value: 2, rest: {value: 1, rest: null}}}} | |
console.log(listToArrayWithHints()); | |
// → [] | |
console.log(listToArrayWithHints(arrayToListWithHints([10, 20, 30]))); | |
// → [10, 20, 30] | |
console.log(listToArrayWithHints({value: 10, rest: {xxx: 20, yyy: null}})); | |
// → [10] | |
console.log(listToArrayWithHints({value: 10, rest: {value: 20, yyy: null}})); | |
// → [10, 20] | |
console.log(listToArrayWithHints({value: 10, rest: {xxx: 20, rest: null}})); | |
// → [10] | |
console.log(nth(arrayToListWithHints([10, 20, 30]), 1)); | |
// → 20 | |
console.log(nth(arrayToListWithHints([10, 20, 30]), 3)); | |
// → undefined | |
console.log(nth(arrayToListWithHints([10, 20, 30]), -2)); | |
// → undefined |
Alright, that makes sense ! Ty for answering :)
Hello Jonurry, I'm a newbie in js so I have some questions about your solutions. I hope you can help me.
In arrayToList function, I don't know how the value of "result" changes in the loop. In your loop, the "result" only appears in the if (typeof currListItem.rest === 'undefined') but when I try testing it like this :
if (typeof currListItem.rest === 'undefined') {
result = newListItem;
console.log(true)
} else {
currListItem.rest = newListItem;
console.log(false)
}
The if (typeof currListItem.rest === 'undefined') only works in the first time, so I don't know how the "result" can change its value.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @PiotrNap. Glad it helped you.
The reason that the first
undefined
is in single quotes is becausetypeof
returns a string value so the comparison has to be a string as well.