title: Create a nested list from array
body: <p>This is the 3rd question from Eloquent JavaScript chapter 4.</p>
  
  <p>Basically it wants me to create a function that will make an array into a nested list from array.</p>
  
  <p>E.g. arrayToList([1, 2, 3]) should return:</p>
  
  <pre><code>var list = {
    value: 1,
    rest: {
      value: 2,
      rest: {
        value: 3,
        rest: null
      }
    }
  };
  </code></pre>
  
  <p>I wonder why my code leads to an infinite loop.</p>
  
  <pre><code>function arrayToList(arr) {
    var list = {};
    for (var i = 0; i &lt; arr.length; i++) {
      var a = arr[i];
      function add(res) {
        if (i == 0) {
          res.value = a;
          res.rest = "null";
        }
        else {
          i -= 1;
          add(res.rest);
        }
      }
      add(list);
    }
    return list;
  }
  </code></pre>
  
  <p>Thank you for taking a look!</p>