2016-06-29 1 views
0

私は現在Eloquent JSの本からJavascriptとDataStructuresを勉強しています。私の現在の割り当ては "要素とリストをとり、その要素をinoputリストの先頭に追加する新しいリストを作成するヘルパ関数prependを書く"です。私は改善したいので、必ずしも私の仕事を探しているわけではありませんが、私を啓発してください。私は3つの質問がありますが、私の主な問題は1番目の問題です。オブジェクトタイプエラーPrepend関数

1:私のコードでは、私のタイプエラーはどこですか?コンソールログに "TypeError:oldObj is undefined"と表示されますが、関数内で初期化します。

lsPre = function(obj) 
{ 
    var iter = 0; 
    var maxIT = 0; //use to capture final  object iteration 
    var oldObj = obj; //initialize old object 

    //create new object to prepend to old object 
    var newObj = {iteration: iter, 
    value: 'Overwrite this Value', 
       next: oldObj.next 
      }; 

    while (oldObj.iteration + 1 > maxIT) 
    { 
     maxIT++; 
     newObj.next = oldObj.next; 
     oldObj = oldObj.next; 
    } 
    return newObj; 
} 

2:より最適な解決策(パラダイム)があり、それから私の意図するプリペンド機能はありますか?私は、より多くのjs指向のパラダイムを学習しようとしており、現在は再帰的な解決法を避けています。

3以下は、上記と同じ問題セットの以前の問題に対するリンクリスト再帰的ソルーションです。

Object {iteration: 1, value: 2, next: Object} 

いうより:それはのようなオブジェクトを返しますが理由です

Object {iteration: 1, value: x, next: Object { iteration: 2, value: x, next: (and so on) }} 

ここでは、コードは次のとおりです。

//Link List 
rayREC = function(ray,iter=0) 
{ //Default param 'inc' allows arbitrary implementation of this function 
    var rLen = ray.length; 
    if (iter > rLen) return; 
    return {//recurively increment objects 
      iteration: iter, 
      value: ray[iter], 
      next: rayREC(ray,iter + 1), //recursion 
      }; 
} 

私も3に最適なパラダイムやソリューションをしたいと思います。

答えて

0

回答1:

おそらくundefinedパラメータで関数を呼び出したので、"TypeError: oldObj is undefined"が得られます。

例えば:

lsPre(); // "TypeError: oldObj is undefined" 

lsPre({iteration: 1, value: 2, next: Object}) // Works... well, kind of... 

回答2:

これは、あなたが実装しようとしているリンクリストの種類によって異なります。例えば

valuenextと本当に簡単にリンクされたリスト:

var simpleListedList = {"value": "Value One", "next": null}; 

あなたはこのいわゆる「リンクリスト」は、独自のノードであることがわかります。

に「プリペンド」、すなわち前にして何かを追加し、我々は単にすることができます:

var oldNode = simpleListedList; 
simpleListedList = {"value": "Value Zero (New)", "next": oldNode}; 

我々はまた、このようbrainlesslyそれを繰り返すことができます:「次ベース」で

var currentNode = simpleListedList; 
while (currentNode != null){ 
    console.log(currentNode.value); 
    currentNode = currentNode.next; 
} 

リンクリスト、それは(すなわちバックに追加)追加し、実際に難しいです:

// First iterate to the last node 
var lastNode = simpleListedList; 
while (lastNode.next != null) { 
    lastNode = lastNode.next; 
} 

// We then add something at the end 
lastNode.next = {"value": "Value N plus One (New last guy)", "next": null} 

// Note that simpleListedList is still the first node, unchanged. 

ANSW er 3:

私は本当にその質問を理解していません。

Object {iteration: 1, value: 2, next: Object} 

は、あなたがJSONでそれを表している場合、それが見えるはずです、

Object {iteration: 1, value: x, next: Object { iteration: 2, value: x, next: (and so on) }} 

これは、適切なリンクリストのようです(反復処理することはできません)next値が一定の対象であると、一つのノードのみです

{ 
    iteration: 1, 
    value: "val of 1", 
    next: { 
     iteration: 2, 
     value: "val of 2", 
     next: { 
      iteration: 3, 
      value: "val of 3", 
      next: { 
       iteration: 4, 
       value: "val of 4", 
       next: { 
        // ... Not sure how you would terminate the linked list 
       } 
      } 
     } 
    } 
} 
+0

回答1まだタイプエラーが発生しています。質問3の関数で作成された新しいリストを考えてみましょう。編集:申し訳ありませんが、モバイルブラウザは私に改行をさせませんので、ここではフォーマットが貧弱です:z = rayRec([1,2,3,4]); lsPre(z); // "TypeError oldObjは未定義です" –

+0

代わりに、 'TypeError:undefined'の反復 'プロパティを読み取れません'が表示されるはずです。これは 'lsPre'のwhileループの中の' oldObj = oldObj.next; 'に起因します。最後のオブジェクトの次は 'null'です。 – kazenorin

+0

それだけです!どうもありがとう! –

関連する問題