Reputation: 61
I am trying to make a full house combination in the game of Yahtzee. (3 of the same dice and 2 of the same)
I have a function that count occurences and will output in here after the random dice generator function:
countDice: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0},
I want to check if the value has a 3 and a 2. And then return 25. I tried to access it with Object.values and some other ways with loops and etc. I tried it with filter but that seems to only work on an object in array and then the rest of my code messes up.
I have tried this as last :
function has(obj, value) {
for(var value in obj) {
if(this.countDice.value === 3 this.countDice.value === 2 ) {
return 25;
}
}
Nothing happens. I have been messing around with every, and some. I am stuck on a simple thing here it seems :s can anyone help me?
Upvotes: 0
Views: 43
Reputation: 3499
Shortest one:
return [2,3].every(x => Object.values(this.countDice).includes(x)) ? 25 : null;
And from your question, if you are using VueJS would be:
data(){
return {
countDice:{1:0,2:0,3:0,4:0,5:0,6:0},
}
},
methods:{
checkCountDice(){
return [2,3].every(x => Object.values(this.countDice).includes(x)) ? 25 : null;
}
}
Upvotes: 2
Reputation: 111
You can directly reference the countDice dictionary and check if both 2 and 3 have non-zero values
let countDice = {1: 0, 2: 9, 3: 7, 4: 0, 5: 0, 6: 0};
let isPresent = countDice[2] && countDice[3];
console.log(Boolean(isPresent));
if (isPresent) {
// Your logic goes here
}
Upvotes: 1