Hey guys. I am trying to build a javascript game doing yahtzee. So far I am able to filter 3’s, 4’s, 5’s, etc. However, now that I’ve gotten to three of a kind. I am lost on how I should approach the problem. I would love to be able to do this with a for loop as I want to become more comfortable with for loops before moving into higher order functions. So far I’ve been programming for 6 months.
This is the class I have built for the rolling system.
class Dice {
constructor(numOfSides){
this.numOfSides = numOfSides;
}
roll(){
return Math.floor(Math.random() * this.numOfSides + 1);
}
}
// then a way to roll 5 dice at once
let multRolls = dice =>{
let diceRoll = new Dice(6)
let fiveDieRoll = []
for(let i = 0; i < dice; i++){
fiveDieRoll.push(diceRoll.roll())
}
return fiveDieRoll
}
//now for attempting to figure out some way to filter 3 of a kind(unfortunately no luck)
function filterThreeDie(x){
let threeOfAKind = []
let roll = multRolls(5)
for(let i = 0; i < roll.length; i++){
if(roll.includes(3) === x){
threeOfAKind.push(roll[i])
}
}
return threeOfAKind
}