Skip to content

Instantly share code, notes, and snippets.

@YannickLeRoux
Created October 16, 2018 05:35
Show Gist options
  • Save YannickLeRoux/65f24c1c55cfd8c98da58db929ce603b to your computer and use it in GitHub Desktop.
Save YannickLeRoux/65f24c1c55cfd8c98da58db929ce603b to your computer and use it in GitHub Desktop.
Eloquent JavaScript - chapter 4
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