#### [SOLVED] How can I run a block of code until get the same result 3 times?

How can I make this code run until getting 3 "Blue" rolls in a row?

``````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')
``````

#### @Joe Warner 2018-04-16 13:10:25

You can just use a variable to count how many times blue has been rolled then stop when it reaches that amount

If you want it to reset when you get a yellow like you said in a comment below add an else to reset the count :)

I need a block that increases a variable each time I get a blue card and when you get a yellow card you reset it

``````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')``````

You can do this without a while loop as well using recursion. my function below isn't the best way to handle it but should give you an idea

``````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')``````

And here is a totally over the top version using es6 classes hopefully anyone should be able to read and guess what is happening.

``````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();``````

#### @ansibly 2018-04-16 14:24:25

I read your initial question to mean: I want a function that will tell me how many times it took to 'roll' the color 'Blue' 3 times.

I need a block that increases a variable each time I get a blue card and when you get a yellow card you reset it

...to mean: I want a function that will tell me how many times it took to 'roll' the color 'Blue' 3 times consecutively.

Here are my take on both using recursive functions instead of loops.

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

#### @goediaz 2018-04-16 13:34:24

I want to share my approach with you.

``````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();``````

I tried to make it as simple as a could.

Here is a fiddle so you can play with it.

#### @Ron Nabuurs 2018-04-16 13:14:18

You have to save a variable when you get an Blue card. For example you can increase a variable each time you get a blue card. Each time you check if that variable is 3 and if it is you have 3 times blue.

``````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++;
}
}``````

#### @b3nc1 2018-04-16 13:13:42

``````    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')
``````

#### @b3nc1 2018-04-16 13:52:21

i was writing it and you were just faster

### [SOLVED] How can I get last characters of a string using JavaScript

• 2011-05-03 18:12:18
• user695663
• 782898 View
• 616 Score
• Tags:   javascript

### [SOLVED] javnumber generator

• 2017-02-03 12:00:46
• Tom Wilson
• 39 View
• -4 Score
• Tags:   javascript

### [SOLVED] How to rewrite code without using jquery?

• 2013-11-14 18:33:11
• Jolanta Smaga
• 102 View
• 0 Score