Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayToList(array) {
- let list = null;
- for (let i = array.length - 1; i >= 0; i--) {
- list = {
- value: array[i],
- rest: list
- };
- }
- return list;
- }
- console.log('arrayToList: ', arrayToList([10, 20]));
- const list = { value: 10, rest: { value: 20, rest: null } };
- function prepend(newElement, list){
- return {
- value: newElement,
- rest: list
- }
- }
- console.log('prepend: ', prepend(5, list));
- function nth(position, list){
- let place = 0;
- while(list.rest){
- if(place === position){
- return list.value;
- }
- list = list.rest;
- place++;
- }
- if(place === position){
- return list.value;
- }
- }
- console.log('nth: ', nth(1, list))
- function listToArray(list){
- let array = [];
- while(list.rest){
- array.push(list.value);
- list = list.rest;
- }
- array.push(list.value);
- return array;
- }
- console.log('listToArray: ', listToArray(list));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement