如何在获得相同结果3次之前运行一段代码?

时间:2018-04-16 13:07:11

标签: javascript arrays

如何运行此代码,直到获得3" Blue"连续滚动?

var cards = ['Blue', 'Yellow'];
var currentCard = 'Yellow';
while (currentCard !== 'Blue') {
  console.log(currentCard);
  var randomNumber = Math.floor(Math.random() * 2);
  currentCard = cards[randomNumber]
}
console.log('Blue')

5 个答案:

答案 0 :(得分:4)

您可以使用变量计算蓝色滚动的次数,然后在达到该值时停止

如果你希望它在你得到黄色时重置,就像你在下面的评论中所说的那样添加一个其他来重置计数:)

  

我需要一个块,每当我拿到一张蓝卡时会增加一个变量,而当你得到一张黄牌时你会重置它



const cards = ['Blue', 'Yellow'];
let currentCard = 'Yellow',
    blueCount = 0;

while (blueCount < 3) {
  const randomNumber = Math.floor(Math.random() * 2);
  console.log(currentCard);
  currentCard = cards[randomNumber];

  if (currentCard === 'Blue') {
    blueCount++;
  } else {
    blueCount = 0;
  }
}

console.log('Blue')
&#13;
&#13;
&#13;

您可以在没有while循环的情况下使用递归来完成此操作。我的下面的功能不是处理它的最好方法,但应该给你一个想法

&#13;
&#13;
var cards = ['Blue', 'Yellow'];

function recursiveRoll3(max = 3, target = 'Blue', count = 0, card = getRandomCard()) {
  if (count === max) return;

  console.log(card)

  return card === target ?
     recursiveRoll3(max, target, count += 1) :
     recursiveRoll3(max, target);
}

function getRandomCard() {
  return cards[Math.floor(Math.random() * 2)];
}

console.log('Find 3 blue rolls then stop')
recursiveRoll3()
console.log('Find 1 yellow roll then stop')
recursiveRoll3(1, 'Yellow')
&#13;
&#13;
&#13;

这是一个完全超过使用es6课程的顶级版本,希望任何人都应该能够阅读并猜测发生了什么。

&#13;
&#13;
class RollTimes {
  constructor({ max = 0, target = null, possibilities = [] }) {
    this.max = max;
    this.target = target;
    this.possibilities = possibilities;
    
    this.count = 0;
    this.currentCard = null;
  }
  
  roll() {
    while (this.notDone) {
      
      this.currentCard = this.getRandomCard();

      console.log(this.currentCard);
      
      this.count = this.hitTarget ? this.count + 1 : 0;
    }
  }

  get notDone() {
    return this.count < this.max;
  }
  
  get hitTarget() {
    return this.currentCard === this.target;
  }
  
  get randomNumber() {
    return Math.floor(Math.random() * this.possibilities.length);
  }
  
  getRandomCard() {
    return this.possibilities[this.randomNumber];
  }
}

const roller = new RollTimes({
  max: 3,
  target: 'Blue',
  possibilities: ['Blue', 'Yellow']
});

roller.roll();
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我读到你的初步问题意思是:我想要一个能告诉我将“蓝色”颜色“滚动”3次所需的次数。

但是,我在一篇文章中读到了你的评论:

  

我需要一个块,每当我拿到一张蓝卡时会增加一个变量,而当你得到一张黄牌时你会重置它

...意思是:我想要一个能告诉我连续3次“滚动”颜色“蓝色”连续的次数。 < / p>

这是我对使用递归函数而不是循环的看法。

function roll(list, target, times, hits, rolls) {
  if(hits === times) return rolls;
  hits = (hits)? hits : 0;
  rolls = (rolls)? rolls : 0;

  var rand = Math.floor(Math.random() * list.length);
  hits = (list[rand] === target)? hits+1 : hits;
  return roll(list, target, times, hits, rolls+1);
}

function rollConsecutive(list, target, times, hits, rolls) {
  if(hits === times) return rolls;
  hits = (hits)? hits : 0;
  rolls = (rolls)? rolls : 0;

  var rand = Math.floor(Math.random() * list.length);
  hits = (list[rand] === target)? hits+1 : 0;
  return rollConsecutive(list, target, times, hits, rolls+1);
}

var numberOfTimesToRollColor = roll(['Blue','Yellow'], 'Blue', 3);
console.log(numberOfTimesToRollColor);

var numberOfTimesToRollColorConsecutively = rollConsecutive(['Blue','Yellow'], 'Blue', 3);
console.log(numberOfTimesToRollColorConsecutively);

答案 2 :(得分:0)

    var cards = ['Blue', 'Yellow'];
    var currentCard = 'Yellow';
    var counter = 0;
    while (counter < 3) {
      console.log(currentCard);
      var randomNumber = Math.floor(Math.random() * 2);
      currentCard = cards[randomNumber]
      if(currentCard === 'Blue'){
        counter++
      }
    }
    console.log('Blue')

答案 3 :(得分:0)

获得蓝卡时必须保存变量。例如,每次获得蓝卡时,您都可以增加变量。每次检查该变量是否为3,如果是3,则为蓝色。

var cards = ['Blue', 'Yellow'];

var timesBlue = 0

var currentCard = 'Yellow';

while (timesBlue != 3) {
    var randomNumber = Math.floor(Math.random() * 2);

    currentCard = cards[randomNumber]
    console.log(currentCard);

    if (currentCard == 'Blue') {
        timesBlue++;
    }
}

答案 4 :(得分:0)

我想和你分享我的方法。

var cards = ['Blue', 'Yellow'];
var count = 0;

function getACard(randomNumber) {
  if (cards[randomNumber] !== 'Blue') {
    console.log(cards[randomNumber]);
    getRandomNumber();
    } else {
        count++
        console.log('Blue: ' + count);
    	if (count < 3) {
          getRandomNumber();
        } else {
      	  return;
      }
    }
}

function getRandomNumber() {
    var randomNumber = Math.floor(Math.random() * 2);
    getACard(randomNumber);
}

getRandomNumber();

我试图让它变得如此简单。

这是一个fiddle所以你可以玩它。