Tell us what’s happening:
I am unable to detect small straight I am doing the following:
If there are exactly 4 unique numbers, I am attempting to find a sequence of 4 consecutive numbers using a sliding window approach. I am generating three possible sets of consecutive numbers {1, 2, 3, 4}, {2, 3, 4, 5}, and {3, 4, 5, 6}, and check if a window of four numbers in the diceValuesArr matches any of these sets.
Your code so far
<!-- file: index.html -->
/* file: script.js */
// User Editable Region
const checkForStraights = (arr) =>{
const counts = {};
for (const num of arr) {
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
let consecutiveCount = 0;
const uniqueNumbers = new Set(arr);
const min = Math.min(...arr);
if(uniqueNumbers.size < 4){
updateRadioOption(5, 0)
}
//for large straight
else if(uniqueNumbers.size === 5){
// only two options possible in our case to get five consecutive numbers when min = 1 or 2
for(let i = min; i<min+2; i++){
for(let j = i; j < 7; j++){
if(counts
[j] == 1){
consecutiveCount++;
if(consecutiveCount === 5){
updateRadioOption(4, 40);
return;
}
}
else{
consecutiveCount =0;
break;
}
}
}
}
//for small straight
// only three options possible in our case to get five consecutive numbers when min = 1 or 2
else if(uniqueNumbers.size === 4){
let consecutiveFourSet = new Set();
let index =0;
// creating 3 different sets of {1, 2, 3, 4}, {2, 3, 4, 5} and {3, 4, 5, 6}
for (let i =1; i<4; i++){
consecutiveFourSet.add(i);
consecutiveFourSet.add(i+1);
consecutiveFourSet.add(i+2);
consecutiveFourSet.add(i+3);
//index = 0 four element window starts from the beginning of the array;
//index = 1 four element window starts from the second index of the array
for(index = 0; index<=1;index++){
// taking a window of four numbers and checking all the numbers in set combination are present in the diceValuesArr
for (let j = index; j < index+4 && j<7; j++){
if(consecutiveFourSet.has(arr[j])){
consecutiveCount++;
if(consecutiveCount === 4)
{
//found if all elements in the set match
updateRadioOption(3, 30);
return;
}
}
else{
consecutiveFourSet.clear();
consecutiveCount =0;
break;
}
}
}
}
}
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:
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 14