Really struggling with this one. Been on it for hours and this is what I’ve got. It seems to me that it isn’t properly running the first if. In my mind if my array is [1,2,3,4,5] then it should work. For example, if arr[i] = 1 then arr[i + 1] would equal 2. 2-1 should equal arr[i]. Am I wrong in thinking this? I’l love some help.
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
const checkForStraights = (arr) => {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] === arr[i + 1] - 1) {
updateRadioOption(4, 40);
updateRadioOption(3, 30);
} else if ([...new Set(arr)].length === 4 && arr[i] === arr[i + 1] - 1) {
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
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 14
Great point. I put arr = arr.sort() at the top. Now it is just telling me I need my small straight. I calculated that but using the new set method. This would get rid of dupes making the array 4 numbers instead of 5. Would that not be correct?
Hmm, I replaced the call in rollDiceBtn callback with checkForStraights([1, 3, 1, 5, 4]), to simulate this case when roll button is clicked. Both option for the Small straight and Large straight got enabled.
Try following step-by-step what happens inside of the function. Taking as the example mentioned [1, 3, 1, 5, 4] array. It’s sorted to [1, 1, 3, 4, 5], noDupes has [1, 3, 4, 5]. What changes when arr is iterated over?
I’m confused as to what you are asking. The loop runs starting at 0, runs as long as i < the length of the arr and goes on to the next i. That would be my answer if you are talking literal.
for (let i = 0; i < arr.length - 1; i++) {
if (noDupes.length === 4 && arr[i] === arr[i + 1] - 1) {
updateRadioOption(3, 30);
} else if (arr[i] === arr[i + 1] - 1) {
updateRadioOption(4, 40);
updateRadioOption(3, 30);
} else {
updateRadioOption(5, 0);
}
}
First i will be 0, therefore arr[i] is 1 and arr[i + 1] also 1. Length of noDupes is 4, but arr[i] !== arr[i + 1] - 1, so only the 5th option is updated. What happens for further i values?
I would assume that the loop continues to run through but since arr[i} !== arr[i + 1] -1 in this case it would not be true meaning that it should not run the code block
Let’s reword this I’m getting lost. I assume you are talking for the array of [1,1,3,4,5]
arr[0] = 1
arr[0+1] = arr[1] which is 1. Then we subtract that and it doesn’t equal.
Are you suggesting that because that the latter part of the array is true, it is saying the if sentence is true?