Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayToList(arr) {
- for (var i=arr.length-1; i >= 0; i--) {
- var list = {value: arr[i], rest: (list || null)};
- }
- return list;
- };
- function listToArray(list) {
- var arr = [];
- for (var node=list; node; node=node.rest)
- arr.push(node.value);
- return arr;
- };
- function prepend(value, rest) {
- var list = {value: null, rest: null};
- list.value = value;
- list.rest = rest;
- return list;
- };
- function nth(list, n) {
- // WHAT IN THE ACTUAL FUCK!!!
- };
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement