The problem
The code runs, but only works for small straights. The count will stop when it reaches 4, and will never reach 5 even when 5 or more consecutive numbers are entered in the array. I assume there is something wrong with my second if
statement. Any help would be really appreciated!
My code
const checkForStraights = (diceValuesArr) => {
let count = 1;
for (let i = 0; i < diceValuesArr.length - 1; i++) {
if (diceValuesArr[i] + 1 === diceValuesArr[i + 1]) {
count++;
}
else if (diceValuesArr[i] + 1 !== diceValuesArr[i + 1]) {
// resets count if a value is not consecutive
count = 1;
}
console.log("count: " + count);
if (count===5) {
updateRadioOption(4,40);
return
}
else if (count===4) {
updateRadioOption(3,30);
return
}
updateRadioOption(5,0);
}}
diceValuesArr = [1,2,3,4,5];
console.log("final output: " + checkForStraights(diceValuesArr))