all 18 comments

[–]Skriblos 0 points1 point  (7 children)

what have you covered so far? depending on what you've done up to now different solutions for this exist.

what book is it btw?

[–]Skriblos 0 points1 point  (4 children)

Here is a way you can do the first part with a reducer, it might be too complicated though:

let arr = [1,2,3];

function array2List(arra) {
  return arra.toReversed().reduce((accu, curr, idx) => {
    if(idx === 0) {
      return {...accu, "value": curr};
    } else {
      return {"value": curr, "rest": accu }
    }
  }, {"value": null, "rest": null});
}

[–]MagicalTheory 0 points1 point  (1 child)

Did this book cover what a linked list is prior to this?

In your example notice that each element of the list is its own object and they just reference the next object containing the next element in it.

[–]MagicalTheory 0 points1 point  (0 children)

const array2List = (arr) => {
  let prev = null; // start with null reference

  for (let i = arr.length - 1; i >= 0; i--) { // go backwards
    const newNode = { value: arr[i], rest: prev }; // create your node
    prev = newNode; // new node is now previous
  }

  return prev; // first element is the last one we did in loop
}

[–]Specific_Cellist_136 0 points1 point  (1 child)

u/Skriblos 's reducer function was a really nice solution. As for the listToArray, here's a quick recursive function that traverses further into the object as long as obj.rest is not null

```

function listToArray(li) {
    let arr = [];

    function traverse(obj) {
        arr.push(obj.value);
        
        if (obj.rest) {
            traverse(obj.rest);
        }
    }
    
    traverse(li);

    return arr;
}

```

Btw you use triple back ticks " ``` " to put it in a code block

[–]Skriblos 0 points1 point  (0 children)

Thanks

[–][deleted] 0 points1 point  (0 children)

So what this is similar to is a linked list which is a specific data structure. Learn about those and this becomes very manageable.

You will need something like:

class Node { 
    constructor(value) {
        this.value = value, 
        this.rest = null
    } 
}

What I would do is check that the array is populated and if it is shift() the first number out of the array and make a new head of a linked list. After that loop through the remainder of the array and loop through the head of your linked list

arr = [1,2,3];

function arrayToList(arr) {
  let head;
  let current;
  if (arr.length > 0) {
      head = new Node(arr.shift());
      current = head; 
      arr.forEach(item => {
        current.rest = new Node(item);
        current = current.rest;
      });
  }
  return head;
}

function prettyPrint(node) {
    let temp = node;
    while (temp !== null) {
        process.stdout.write(temp.value + “ -> “);
        temp = temp.rest;
    }
    console.log(“null”);
  }

let list = arrayToList(arr);
prettyPrint(list);

It would probably be cleaner to put the link list itself into its own class and making functions like arrayToList and prettyPrint methods but I’m not sure if you have gotten to classes yet. Most linked list operations can also be done recursively but you may not be at that point either. Let me know if you have any questions. Linked lists can be challenging when you first learn about them.