I'm reading through Eloquent JavaScript and at the end of Chapter 4 it challenges you to turn an array into a list i.e.
[1, 3, 3] --> {value: 1, rest: {value: 2, rest: {value: 3, rest: null}}};
And I do not think I'm understanding the concept.
It suggested iterating backward through the array, so I had tried:
function arrayToList(array) {
let list = {};
for (let i = array.length -1; i > array[0]; i--) {
list += ("value:" + i + ", rest: null");
}
return list;
}
console.log(arrayToList([1, 2, 3]));
And this logs out:
[object Object]value:2, rest: null
As you can see I'm not understanding how to create "nested" objects (lists) with iteration. Can someone please explain this to me?