簡體   English   中英

如何重組.forEach,這樣我才能突圍而出

[英]How to restructure .forEach so I can break out of it

我在使用來自Ruby背景的JavaScript和“ break”語句時遇到了很多麻煩。

這是我的功能:

function isItTheNumber(numberToGuess){

    if(previousGuess === null){
      previousGuess = [playersGuess];
    }

    previousGuess.forEach(function(guess){
      if(playersGuess === guess && previousGuess > 1){
        textStatus.style.display = 'block';
        textStatus.innerHTML = 'You already picked that number';
      } else if(parseInt(playersGuess) === parseInt(numberToGuess)){
        textStatus.style.display ='block';
        textStatus.innerHTML = 'You Are CORRECT!';    
      } else {
      previousGuess.push(playersGuess);
      textStatus.style.display='block';
      textStatus.innerHTML = 'You are ' + hotOrCold();
      document.getElementById('guess-count').innerHTML = playerGuessCount++;
    }
  });
}

在我的.forEach循環中,我想在我的第一個if語句中使用“ break”語句。 我希望如果執行此塊,則循環會停止。

在閱讀了一些關於它的帖子后,我意識到我無法在此forEach函數中使用break語句。 我在這里嘗試使用“每個”的建議但是當我將其包裝在函數中時,我無法返回true或false值。

我想避免必須使用任何類型的“中斷”或hack進行中斷,但是如果這是唯一的方法,它將使用它。 如果有人對我如何重新設計邏輯有任何建議,或者有任何建議,我將不勝感激。 我將在下面的偽代碼中列出我的邏輯。

1) Check if the previousGuess array is null or populated. If it is null, set it to an array with the first value.
2) Iterate over the previousGuess array.
3) If: see if the user input (playerGuess) is in the previousGuess array. The previous guess
array must be larger than 1.
4) Else If: If the users guess is the same as the a the value, tell them they are correct.
5) Else: if the users guess isn't the same as the value, tell them and add 1 to the playerGuessCount.

我當前邏輯的問題是,playerGuessCount被調用了太多次。 如果要遍歷數組並找到並且第一個if語句為true,它將仍然遍歷數組的其余部分,即使他們僅提交1個猜測,也要在playerGuessCount上加1。 嚴格地存在.forEach來檢查他們的猜測是否重復。

這是我對“每一個”的嘗試http://repl.it/P74

您使用的.forEach方法是通過擴展函數原型實現的包裝器。

您可以打破常規循環:

for (var key in objs){
  var obj=objs[key];
  //do your business here

  break; //this line exists the loop
}

但是,如果使用了回調函數,則必須通過放置“ skip”變量來終止函數調用本身。

previousGuess.forEach(function(guess){
  if (this.skip_the_foreach_loop) return;
  if (need_to_exit) this.skip_the_foreach_loop=true;
});

這不是最有效的方法,但是可以節省一些CPU周期。

這會為您要嘗試的工作嗎?

function isItTheNumber(numberToGuess){

    if(previousGuess === null){
      previousGuess = [playersGuess];
    }

    // Using Array.prototype.every so a falsey return breaks
    previousGuess.every(function(guess){
      if(playersGuess === guess && previousGuess > 1){
        textStatus.style.display = 'block';
        textStatus.innerHTML = 'You already picked that number';
        return; // returns undefined which is falsey and breaks loop.
      } else if(parseInt(playersGuess) === parseInt(numberToGuess)){
        textStatus.style.display ='block';
        textStatus.innerHTML = 'You Are CORRECT!';    
      } else {
        previousGuess.push(playersGuess);
        textStatus.style.display='block';
        textStatus.innerHTML = 'You are ' + hotOrCold();
        document.getElementById('guess-count').innerHTML = playerGuessCount++;
      }
      return true; // Meaning go to next iteration
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM