Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. function arrayToList(array) {
  2. let list = null;
  3. for (let i = array.length - 1; i >= 0; i--) {
  4. list = {
  5. value: array[i],
  6. rest: list
  7. };
  8. }
  9. return list;
  10. }
  11.  
  12. console.log('arrayToList: ', arrayToList([10, 20]));
  13.  
  14. const list = { value: 10, rest: { value: 20, rest: null } };
  15.  
  16. function prepend(newElement, list){
  17. return {
  18. value: newElement,
  19. rest: list
  20. }
  21. }
  22.  
  23. console.log('prepend: ', prepend(5, list));
  24.  
  25. function nth(position, list){
  26. let place = 0;
  27. while(list.rest){
  28. if(place === position){
  29. return list.value;
  30. }
  31. list = list.rest;
  32. place++;
  33. }
  34. if(place === position){
  35. return list.value;
  36. }
  37. }
  38.  
  39. console.log('nth: ', nth(1, list))
  40.  
  41. function listToArray(list){
  42. let array = [];
  43. while(list.rest){
  44. array.push(list.value);
  45. list = list.rest;
  46. }
  47. array.push(list.value);
  48. return array;
  49. }
  50.  
  51. console.log('listToArray: ', listToArray(list));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement