亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript Recursion inside class method(此行為)

Javascript Recursion inside class method(此行為)

開滿天機 2022-09-02 19:42:25
問題在于 toString 函數內部,這并沒有實現函數調用自己的范圍:我需要一些東西來解決問題,'這'是javascript中最糟糕的,我已經在python中實現了確切的數據結構和函數,并且它有效...,我已經嘗試過綁定和箭頭函數,也許你可以幫我解決...代碼的預期結果如下:1|__2   |__3      |__4         |__5class Node {    constructor(data){        this.data = data;        this.parent = null;        this.children = [];        Node.nodes.push(this);        this.index = Node.nodes.length;    }    addChild(node){        node.parent = this;        this.children.push(node);    }    getLevel(){        let level = 0;        while(this.parent){            level+=1;            this.parent = this.parent.parent;        }        //console.log('lvl',level);        return level;    }     toString (){        // console.log('lvl',this.getLevel());        let prefix = " ".repeat(this.getLevel()*3);        prefix += this.getLevel()===0 ? "" :"|__";        console.log(prefix + this.index);        if(this.children){            for(let i = 0; i < this.children.length; i++){                this.children[i].toString();            }        }    }    pathToRoot(){        return 0;    }}Node.nodes = [];const main = (()=>{let root = new Node('root');let kid1 = new Node('kid1');let kid2 = new Node('kid2');let kid3 = new Node('kid3');let kid4 = new Node('kid4');root.addChild(kid1);kid1.addChild(kid2);kid2.addChild(kid3);kid3.addChild(kid4);console.log('kid4 lvl :',kid4.getLevel())root.toString(root);})()
查看完整描述

2 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

在循環中分配父項的父項。而是使用變量來循環父項。


class Node {


    constructor(data) {

        this.data = data;

        this.parent = null;

        this.children = [];

        if (!Node.nodes) Node.nodes = [];

        Node.nodes.push(this);

        this.index = Node.nodes.length;

    }

   

    addChild(node) {

        node.parent = this;

        this.children.push(node);

    }


    getLevel() {

        let level = 0;

        let parent = this.parent;   // start with parent

        while (parent) {            // check value

            level += 1;

            parent = parent.parent; // assign parent

        }

        //console.log('lvl',level);

        return level;

    }


    toString() {

        // console.log('lvl',this.getLevel());

        let prefix = " ".repeat(this.getLevel() * 3);

        prefix += this.getLevel() === 0 ? "" : "|__";

        console.log(prefix + this.index);


        if (this.children) {

            for (let i = 0; i < this.children.length; i++) {

                this.children[i].toString();

            }

        }

    }


    pathToRoot() {

        return 0;

    }


}


const main = (() => {

  let root = new Node('root');

  let kid1 = new Node('kid1');

  let kid2 = new Node('kid2');

  let kid3 = new Node('kid3');

  let kid4 = new Node('kid4');


  root.addChild(kid1);

  kid1.addChild(kid2);

  kid2.addChild(kid3);

  kid3.addChild(kid4);


  console.log('kid4 lvl :', kid4.getLevel())


  root.toString(root);

console.log(root);

})();

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2022-09-02
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

這似乎與機制無關。你缺少一個基本情況(所以遞歸函數將永遠保持運行)。您需要考慮遞歸應該在什么情況下結束。this


toString (){

        // think of your base case here. Examples are as below

        // should it be prefix === ""?

        // should it be when length of prefix smaller or greater than specific length 



        let prefix = " ".repeat(this.getLevel()*3);

        prefix += this.getLevel()===0 ? "" :"|__";

        console.log(prefix + this.index);


        if(this.children){

            for(let i = 0; i < this.children.length; i++){

                this.children[i].toString();

            }

        }

    }


查看完整回答
反對 回復 2022-09-02
  • 2 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號