Created
October 16, 2018 05:35
-
-
Save YannickLeRoux/65f24c1c55cfd8c98da58db929ce603b to your computer and use it in GitHub Desktop.
Eloquent JavaScript - chapter 4
This file contains hidden or 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(arr) { | |
let list = { value: arr[arr.length - 1], rest: null }; | |
for ( let i=arr.length - 1; i>=0; i--) { | |
list = { value: arr[i], rest: list} | |
} | |
return list; | |
} | |
console.log(arrayToList([1,2,3])); | |
function listToArray(list) { | |
let arr = []; | |
while(list.rest) { | |
arr.push(list.value); | |
list = list.rest; | |
} | |
return arr; | |
} | |
console.log(listToArray(arrayToList([1,2,3]))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment