Question on Eloquent Javascript problem - A List

Hello everyone,
I’m learning Javascript on Eloquent Javascript as well as my additional resource.

I have a question on chapter 4 excercise called A list. the exercise have few sub exercise in it. but for now I’ll just discuss about the first sub exercise.

so what they require me to do is that:

Write a function arrayToList that builds up a list structure. to get the picture of it, when we write code:
console.log(arrayToList([10, 20]));
the expected result will be: {value: 10, rest: {value: 20, rest: null}}

to make it work, this code is written:

function arrayToList(array) {
  let list = null;
  for (let i = array.length - 1; i >= 0; i--) {
    list = {value: array[i], rest: list};
  }
  return list;
}

arrayToList([10, 20])
//->{value: 10, rest: {value: 20, rest: null}}

so my question is:

  1. why does it need to do backwards iteration?
  2. since it’s backward iteration, isn’t that the first value of array[i] would be 20? so why the result isnt {value: 20, rest: {value: 10, rest: null}}?

Because of the way you are building it. You are adding the old list to the “rest” property and then wrapping that in a new object and making that the new list.

It’s like if you are making a turduken - a chicken stuffed in a duck stuffed in a turkey. Since your function is to “wrap” like this, you have to start with the inside and work your way out. You couldn’t start with the turkey, wrap it in a duck and wrap that in the chicken - that would be inside out. You’ve got to start and the end and work backwards.


function arrayToList(array) {
  let list = null;
  for (let i = array.length - 1; i >= 0; i--) {
    list = {value: array[i], rest: list};
  }
  return list;
}

const list = arrayToList(['turkey', 'duck', 'chicken'])
console.log(JSON.stringify(list, null, 2))
// {
//   "value": "turkey",
//   "rest": {
//     "value": "duck",
//     "rest": {
//       "value": "chicken",
//       "rest": null
//     }
//   }
// }

In order to get it to work the way you want with an ascending for, I think you’d need to keep a reference to the tail of your list, to keep track of where to add the new element, because you would be adding to the tail of the list and not the head. It shouldn’t be too hard, but it might take a tiny bit more code.

1 Like

Or, to watch what is happening…

function arrayToList(array) {
  let list = null;
  for (let i = array.length - 1; i >= 0; i--) {
    console.log('*** adding', array[i])
    list = {value: array[i], rest: list};
    console.log('*** new list', JSON.stringify(list, null, 2))
  }
  return list;
}

const list = arrayToList(['turkey', 'duck', 'chicken'])
console.log('\nFinal List:\n')
console.log(JSON.stringify(list, null, 2))
1 Like

Thanks! now I understand. just to be sure so it was because of the list variable wrapped into a new list variable that make the code works like turduken turkey right?

It works because of the way you are building your list. You are taking the old list, and making it a new list with the new element. You are adding to the front of the list and pushing the rest back. So, you have to start with the “last” item that you want.

1 Like

Okeii got it thankssss