2017-01-04 13 views
2

データ構造とAlgosをO'Reilyが処理しています。私は既にリスト関数を書き出しています。私のappendをテストして機能を削除しています。私の削除機能は実際には機能しませんし、私は本の中にあるものを使いました。私は本の中で、「レイモンド」がリストから削除されていないようjavascriptのリストの削除関数を作成しましたが、要素を削除していません

names.remove("Raymond"); 

を実行しようとすると

function remove(element) 
{ 
    var foundAt = this.find(element); 
    if (foundAt > -1) 
    { 
     // splice(index, howmany, item1, ...., itemX) 
     this.dataStore.splice(foundAt, 1); 
     --this.listSize; 
     return true; 
    } 
    return false; 
} 

:ここでは関数です。私は何が間違っているのかわからない。

function List() { 
 
     this.listSize = 0; 
 
     this.pos = 0; 
 
     this.dataStore = []; //initializes an empty array to store list  elements 
 
     //this.clear = clear; 
 
     this.find = find; 
 
     this.toString = toString; 
 
     //this.insert = insert; 
 
     this.append = append; 
 
     this.remove = remove; 
 
     //this.front = front; 
 
     //this.end = end; 
 
     //this.prev = prev; 
 
     //this.next = next; 
 
     this.length = length; 
 
     //this.currPost = currPos; 
 
     //this.moveTo = moveTo; 
 
     //this.getElement = getElement; 
 
     this.length = length; 
 
     //this.contains = contains; 
 
    } 
 

 
    // append an element 
 
    function append(element) { 
 
     this.dataStore[this.listSize++] = element; 
 
    } 
 

 
    // remove an element 
 
    function find(element) { 
 
     for (var i = 0; i < this.dataStore; ++i) { 
 
     if (this.dataStore[i] == element) { 
 
      return i; 
 
     } 
 
     } 
 
     return -1; 
 
    } 
 

 
    // after the array is modified 
 
    // listSize is decremented by 1 to reflect the size of the list 
 
    function remove(element) { 
 
     var foundAt = this.find(element); 
 
     if (foundAt > -1) { 
 
     // splice(index, howmany, item1, ...., itemX) 
 
     this.dataStore.splice(foundAt, 1); 
 
     --this.listSize; 
 
     return true; 
 
     } 
 
     return false; 
 
    } 
 

 
    function length() { 
 
     return this.listSize; 
 
    } 
 

 
    function toString() { 
 
     return this.dataStore; 
 
    } 
 

 
    var names = new List(); 
 
    names.append("Cynthia"); 
 
    names.append("Raymond"); 
 
    names.append("Barbara"); 
 
    console.log(names.toString()); 
 
    names.remove("Raymond"); 
 
    console.log(names.toString());

答えて

3

何もあなたのfind方法のループが間違っているため、削除されています:ここに私のコードです。

変更:へ

for (var i = 0; i < this.dataStore; ++i) 

for (var i = 0; i < this.dataStore.length; ++i) 

固定https://jsfiddle.net/vjd7zocd/

+0

ありがとう!私は私が推測することを見続けた:( –

関連する問題