如何重组.forEach所以我可以摆脱它

时间:2014-03-05 02:29:38

标签: javascript arrays foreach

我在使用来自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语句。我尝试了使用“every”的建议here但是当我在函数中使用它时,我无法返回true或false值。

我想避免使用任何类型的“休息”或破解,但如果它是唯一的方法将使用它。如果有人对如何重新构建我的逻辑或有任何建议有任何建议我会很感激。我将在下面的伪代码中列出我的逻辑。

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

的尝试

2 个答案:

答案 0 :(得分:3)

您正在使用的.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周期。

答案 1 :(得分:0)

这会对您要做的事情有用吗?

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
  });
}