Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. // Your code here.
  2.  
  3. arrayToList = arr => {
  4. doStuff = (arr, obj) => {
  5. if(arr.length){
  6. console.log('arr ',arr)
  7. obj.value = arr[0];
  8. obj.rest = {};
  9. arr.shift();
  10. return (doStuff(arr, obj.rest));
  11. }else{
  12. console.log('else ', obj);
  13. obj.rest = {value : arr[0], rest : null}
  14. return obj;
  15. }
  16. }
  17. obj = {};
  18. doStuff(arr, obj);
  19. return obj;
  20. }
  21.  
  22. console.log(arrayToList([10, 20]));
  23. // → {value: 10, rest: {value: 20, rest: null}}
  24. //console.log(listToArray(arrayToList([10, 20, 30])));
  25. // → [10, 20, 30]
  26. //console.log(prepend(10, prepend(20, null)));
  27. // → {value: 10, rest: {value: 20, rest: null}}
  28. //console.log(nth(arrayToList([10, 20, 30]), 1));
  29. // → 20
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement