If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30.
If a large straight is rolled, your checkForStraights function should enable the fifth radio button, set the value to 40, and update the displayed text to , score = 40.
If a large straight is rolled, your checkForStraights function should also enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30
I have tested this manually by passing a straights array and non straights. It’s able detect both small and large straights and call the updateRadioOption() with the right arguments. I’m having trouble figuring out what’s wrong with the code.
Your code so far
<!-- file: index.html -->
/* file: script.js */
// User Editable Region
const checkForStraights = (arr) => {
const tally = {};
for (let i = 1; i < arr.length; i++) {
if (arr[i] - arr[i - 1] === 1) {
tally["true"] = tally["true"] ? tally["true"] + 1 : (tally["true"] = 1);
}
}
if (tally["true"] === 4) {
updateRadioOption(4, 40);
}
if (tally["true"] >= 3) {
updateRadioOption(3, 30)
} else { updateRadioOption(5, 0) };
};
rollDiceBtn.addEventListener("click", () => {
if (rolls === 3) {
alert("You have made three rolls this round. Please select a score.");
} else {
rolls++;
resetRadioOptions();
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
detectFullHouse(diceValuesArr);
checkForStraights(diceValuesArr);
// User Editable Region
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 14
first thing is you will need to move this update out of the else. You want this to happen all the time when this function is called.
[quote=“Arlen, post:1, topic:713961”]
Next, read the exercise again. They said that a large straight is
A large straight is when all five dice have consecutive values in any order
5 dice, not 4
and small straight is 4 dice, not 3 also a large straight by definition, includes a small one. (so 2 radio buttons will need to be updated in the case of 5 dice forming a straight)
Edit: I think your code will work to set 2 radio buttons on for a large straight given the >= comparison
Wouldn’t you have to sort the array first for this to work?
As said, if you get a large straight, you have to enable the radio buttons for both small and large.
Aside, you don’t need the assignment for falsy in the ternary condition because you already have an assignment before the ternary. The conditions just need to evaluate to a value (i.e. 1 for the falsy condition).
the for loop above subtracts a value from an array to the one before it and make sure that it’s equal to 1. In a five digit value there are only up to four comparisons. So if there are three true it’s small straight and if there’s four it’s a large straight. I’m probably wrong thought. I’m not really sure .
That’s fine, I didn’t check your algorithm closely as I was just going on first impressions. You definitely should confirm your code works with something like [5,6,4,2,3]
Yes. The order of the dice doesn’t matter. Just like how a pair in poker isn’t dependent on how the cards are ordered.
Although, it does say “consecutive values”, so I’m not sure if it really means that or not?
Edit: If this was a real life dice game, how would you roll the dice so they not only had a sequence but landed in the correct order? I mean, that is pretty much impossible.
So I was solving a different a different problem. It said consecutive so I thought its descending or ascending. I didn’t know the order shouldn’t matter at all. Thank you for explaining.