首页 > 解决方案 > 我不理解雄辩的 js“列表”练习(我有一些奇怪的结果)

问题描述

我现在正在做 EloquentJS 书中的练习。第 4 章,“列表”练习,更准确地说。该练习包括从数组创建和解构列表。

我尝试了一些对我来说看起来很合乎逻辑的东西,但我得到了我不明白的回报。我得到一个圆形标签/标记。

这是我的第一次尝试和第二次分别尝试:

function arrayToListTry1 (array) {
  /**My solution try 1
  My observations:
  I was building it inverted (last elem in first place
  I don't understand why my version gets a circular result and the book solution doesn't
  */
    let list = {
        value: "",
        rest: {}
    };

    for (let i = 0; i < array.length; i++) {
        list.rest = list;
        list.value = array[i];
    }

    return list;
}
/**result: 
node eloquentJS_chapter4_ex_arrayToList.js
{ rest: [Circular], value: 'hello' }
*/

function arrayToListTry2 (array) {

    let list = {};

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

//same result

练习解决方案显示了我检查过的另一种方式,但不清楚我的解决方案有什么问题。我还检查了 stackoverflow 上其他人对这个练习的看法,但没有找到我理解的解释。

这是实际的锻炼解决方案:

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

/**
result:
node eloquentJS_chapter4_ex_arrayToList.js
{ value: 'hello',
  rest: { value: 'world', rest: { value: '!!!', rest: null } } }
*/

编辑:这是我的最终结果,我将跳过整个文件内容。感谢大家。

let listValue = {
    value: "hello",
    rest: {
        value: "world",
        rest: {
            value: "!!!",
            rest: {}
        }
    }
};

const arrayValue = ["hello", "world", "!!!"];

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

  /**My solution try 1
  My observations:
  I was building it inverted (last elem in first place
  I dont understand why my version gets a circular result and the book solution doesnt
  */
//  let list = {
// //       value: "",
// //       rest: {
// //
// //       }
//  };
//
//  for (let i = 0; i < array.length; i++) {
//      list.rest = list;
//      list.value = array[i];
//      // console.log(list);
//      // console.log(list.rest);
//  }
//  //console.log(list.rest);
//  // console.log("result:");
//  return list;

  /** try 2
  */
// let list = {};
//
// for (let i = array.length - 1; i >= 0; i--) {
//  list.rest = list;
//  list.value = array[i];
// }
// return list;

/** try 3
    Every loop creates a new list object containing the old one.
    But i still dont see why my tries above don't work.
*/
    let list = {};

    for (let i = array.length - 1; i >= 0; i--) {
        list = {
            value: array[i],
            rest: list
        }
        // console.log(list);
    }

    return list;
}
/***
An other possibility that relate more to my thought process. Given to me on stackoverflow
function arrayToListTry4 (array) {
    let list = {}
    for (let i = array.length - 1; i >= 0; i--) {
        list.rest = Object.assign({}, list);
        list.value = array[i];
    }

    return list
}
**/


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

    while (list.rest !== undefined ) {
        arr.push(list.value);
        list = list.rest;
        // console.log(list);
        // console.log(arr);
    }

    return arr;
}


function prepend (item, list){

    return { value: item, rest: list};
}


function nth (list, nth){
    for (var i = 0; i < nth -1; i++) {
        list = list.rest
    }

    return list.value
}


function recNth (list, nth){

    if (nth - 1 == 0) {
        return list.value;
    } else {
        return recGetNth(list.rest, nth -1);
    }
}


console.log(arrayToList(arrayValue));
// → { value: 'hello',
       rest: { value: 'world', rest: { value: '!!!', rest: {} } } }

console.log(listToArray(list));
// → [ 'hello', 'world', '!!!' ]

console.log(listToArray(arrayToList(arrayValue)));
// → [ 'hello', 'world', '!!!' ]

console.log(arrayToList(listToArray(listValue)));
// → { value: 'hello',
       rest: { value: 'world', rest: { value: '!!!', rest: {} } } }

console.log(prepend("Hey", listValue));
// → { value: 'Hey',
       rest: { value: 'hello', rest: { value: 'world', rest: { value: '!!!', rest: {} } } } }

console.log(nth(listValue, 3));
// → "!!!"

console.log(recNth(listValue, 2));
// → "world"

标签: javascript

解决方案


你的for循环基本上是这个语句:

list.rest = list; // this gets constantly reassigned
list.value = array[array.length - 1];

rest和 一样,list有一个循环,JS 向你展示这个循环。

book 解决方案通过创建新列表而不是一次又一次地重用同一个列表来避免这个问题。


推荐阅读