如何重组.forEach这样我就可以摆脱它 [英] How to restructure .forEach so I can break out of it

查看:129
本文介绍了如何重组.forEach这样我就可以摆脱它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有很多的麻烦,JavaScript和从红宝石背景的'破'的声明。

I have had a lot of trouble with JavaScript and 'break' statements coming from a Ruby background.

下面是我的功能:

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语句一个休息的语句。我想循环停止,如果它曾经执行此块。

In my .forEach loop I would like to have a 'break' statement in my first if statement. I would like the loop to stop if it ever executes this block.

我意识到我不能使用此功能的forEach break语句读了几帖吧了。我试图在这里的建议 使用每一个,但我是用这个的时候裹在的功能,我没能返回true或false值。

I realize I can't use a break statement in this forEach function after reading a few posts about it. I attempted the suggestion here using "every" but when I was using this wrapped in a function I was not able to return a true or false value.

我想避免使用任何类型的休息或黑客的破解,但会使用它,如果它是唯一的出路。如果任何人有我如何能有任何建议重新工作,我的逻辑或者有任何建议,我将AP preciate它。我会列出我在下面的伪code逻辑。

I would like to avoid having to use any type of 'break' or hack for the break but will use it if it is the only way. If anyone has any suggestions on how I can re-work my logic or have any suggestions I would appreciate it. I'll list my logic in pseudo code below.

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语句是真实的,它仍然会遍历数组的其余部分,加入1到playerGuessCount即使他们只能提交1猜测。该.forEach严格那里检查,如果他们的猜测是重复。

The problem with my current logic is that the playerGuessCount is being invoked too many times. If the array is being iterated over and finds and the first if statement is true, it will still iterate over the rest of the array, adding 1 to the playerGuessCount even when they only submit 1 guess. The .forEach is strictly there to check if their guess is a repeat.

这是我尝试用每一个 http://repl.it/P74

推荐答案

您正在使用的.forEach方法是通过扩展函数原型实现的包装。

The .forEach method you are using is a wrapper implemented by extending a function prototype.

您可以打破传统的循环:

You can break out of a conventional loop:

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

  break; //this line exists the loop
}

然而,如果使用一个回调函数,你必须终止函数放置一个跳过变量调用自己。

However, if a callback function is used, you have to terminate the function call itself by placing a "skip" variable.

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

不是最有效的方式,但可以节省你几个CPU周期。

Not the most efficient way but saves you a few CPU cycles.

这篇关于如何重组.forEach这样我就可以摆脱它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆