Last active
November 8, 2016 16:22
-
-
Save oliverjam/b30c59dd73562f75ea7879011d0b07c4 to your computer and use it in GitHub Desktop.
EJ: C4 P3 created by oliverjam - https://repl.it/ESNK/1
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(array) { | |
| let list = null; | |
| for (let i = array.length - 1; i >= 0; i--) { | |
| list = { value: array[i], rest: list }; | |
| } | |
| return list; | |
| } | |
| function listToArray(list) { | |
| let result = []; | |
| for (let node = list; node; node = node.rest) { | |
| result.push(node.value); | |
| } | |
| return result; | |
| } | |
| function prepend(val, list) { | |
| list = { value: val, rest: list }; | |
| return list; | |
| } | |
| function nth(list, index) { | |
| for (let node = list; node; node.rest) { | |
| index -= 1; | |
| if (index === 0) return node.value; | |
| } | |
| } | |
| console.log(arrayToList([10, 20, 30, 40, 50])); | |
| console.log(listToArray(arrayToList([10, 20, 30]))); | |
| console.log(prepend(10, prepend(20, null))); | |
| console.log(nth(arrayToList([10, 20, 30]), 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment