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

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

我怎樣才能使這個函數遞歸/運行,直到不滿足要求?

我怎樣才能使這個函數遞歸/運行,直到不滿足要求?

慕蓋茨4494581 2021-11-25 16:43:12
到目前為止,我只能在每個 if 語句之后硬編碼相同的代碼段,只需要更改 getAdjacentCells(id) 的參數即可。我一直無法找到重復這部分的方法。我認為這可以遞歸完成,但我不知道該怎么做。編輯:我最初輸入 isCellEmpty 得到了一個對象數組:[{topLeft: null}, {topCenter: "cell-1-2"}, {topRight: "cell-1-3"}, {middleLeft: null}, {middleRight: "cell-2-3"}],當實際上是單個對象時:{topLeft: null, topCenter: "cell-1-2", topRight: "cell-1-3", middleLeft: null , middleRight: "cell-2-3"}// Gets an object that looks like this: {topLeft: null, topCenter: "cell-1-2", topRight: "cell-1-3", middleLeft: null, middleRight: "cell-2-3"}function isCellEmpty(adjacentCells) {  Object.values(adjacentCells).forEach(id => {    // Checks that the ids in stored in the object values do not equal null    if (id !== null) {      board[getBoardPosition(id)].opened = true;      // getAdjacentCells() will return either an array of objects similar to the one the function takes as an argument or an integer      // if getAdjacentCells(id) returns a number, add a div to the HTML element with that id      if (typeof (getAdjacentCells(id)) === "number") {        // Removes all other divs, this prevents repetition        $("#" + id).empty();        // Appends an empty div        $("#" + id).append("<div></div>");      // HERE'S WHERE IT STARTS: If getAdjacentCells(id) returns an object, do the same as above with every id in it      } else if (typeof (getAdjacentCells(id)) === "object") {        Object.values(getAdjacentCells(id)).forEach(id2 => {          if (id2 !== null) {            board[getBoardPosition(id2)].opened = true;            if (typeof (getAdjacentCells(id2)) === "number") {              $("#" + id2).empty();              $("#" + id2).append("<div></div>");            // HERE IT REPEATS:             } else if (typeof (getAdjacentCells(id2)) === "object") {              ...             }          }        })      }    }  });}
查看完整描述

2 回答

?
MMMHUHU

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

您可以使用從 中獲得的值進行遞歸調用getAdjacentCells。但是,請確保getAdjacentCells只為相同的id. 現在,當您重復相同的調用時,效率非常低。


另請參閱代碼中的其他一些建議。


function isCellEmpty(adjacentCells) {

    // I would move this check here, although not necessary if you prefer it in the loop.

    if (typeof adjacentCells === "number") {

        $("#" + id).empty().append("<div>"); // You can chain jQuery...

        return;

    } 

    for (let id of adjacentCells) { // Just use a for..of loop

        if (id === null) continue; // keep IF-ELSE nesting flat.

        let cell = board[getBoardPosition(id)];

        if (cell.opened) continue; // Add this to avoid circling around

        cell.opened = true;

        isCellEmpty(getAdjacentCells(id)); // recursive call

    }

}

對象值

您在代碼的注釋中寫道:


getAdjacentCells() 將返回類似于函數作為參數的對象數組或整數


但是,您在此答案下方的評論似乎表明情況并非(總是)。它可能是一個簡單的對象,可以解釋你為什么Object.values要迭代它。如果是這種情況,我會敦促進行更改,getAdjacentCells以便它確實返回一個數組。或者,如果這是不可能的,那么Object.values像你已經做過的那樣使用:


function isCellEmpty(adjacentCells) {

    // I would move this check here, although not necessary if you prefer it in the loop.

    if (typeof adjacentCells === "number") {

        $("#" + id).empty().append("<div>"); // You can chain jQuery...

        return;

    } 

    for (let id of Object.values(adjacentCells)) { // Just use a for..of loop

        if (id === null) continue; // keep IF-ELSE nesting flat.

        let cell = board[getBoardPosition(id)];

        if (cell.opened) continue; // Add this to avoid circling around

        cell.opened = true;

        isCellEmpty(getAdjacentCells(id)); // recursive call

    }

}


查看完整回答
反對 回復 2021-11-25
?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

遞歸在這里應該可以正常工作:在最基本的情況下,您可以使用id2. 但是,假設getAdjacentCells可能會返回您已經訪問過的單元格,除非您能夠跟蹤已訪問過的 ID 并將其傳入,否則您最終將無限遞歸。


function setCellState(id, visited) {

  if(id === null) {

    return;

  }

  if(visited === undefined) {

    visited = new Set();

  }

  if(visited.has(id)) {

    return;

  }

  visited.add(id);


  board[getBoardPosition(id)].opened = true;


  // getAdjacentCells() will return either an array of objects similar to the one the function takes as an argument or an integer

  let adjacentCells = getAdjacentCells(id);

  // if getAdjacentCells(id) returns a number, add a div to the HTML element with that id

  if (typeof (adjacentCells) === "number") {

        // Removes all other divs, this prevents repetition

        $("#" + id).empty()

          // Appends an empty div

          .append("<div></div>");

  } else if (typeof (adjacentCells) === "object") {

    Object.values(adjacentCells).forEach(id2 => setCellState(id2, visited));

  }

我冒昧地更改了方法名稱,以便更能代表該方法的實際作用。我還更改了它以從單個單元格的 ID 開始,因為這簡化了遞歸并允許圍繞 的行為進行注釋getAdjacentCells以提供更好的上下文。


查看完整回答
反對 回復 2021-11-25
  • 2 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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