#### [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

``````    var cards = ['Blue', 'Yellow'];
var currentCard = 'Yellow';
var blueCount = 0;
while (blueCount < 3) {
console.log(currentCard);
var randomNumber = Math.floor(Math.random() * 2);
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 i'd suggest 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(count = 0, card = getRand()) {
if (count === 3) return;
console.log(card)
return card === 'Blue' ?
recursiveRoll3(count += 1) :
recursiveRoll3(0);
}

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

#### @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
• 709236 View
• 562 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
• 100 View
• 0 Score