Hi guys, i have tried a lot of different ways to solve this, but still can not pass step 2 and 4. Is there anyone who can please look at my code and provide some hints so i can solve this? Thank you!
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
function checkForStraights(arr) {
const newArr = arr.toSorted((a, b) => a - b)
for(let i = 1; i < newArr.slice(0, 4).length; i++){
if(newArr[i] - newArr[i-1] === 1){
updateRadioOption(3, 30);
scoreInputs[4].disabled = false;
}
updateRadioOption(5, 0);
}
for(let i = 1; i< newArr.length; i++){
if(newArr[i] - newArr[i-1] === 1){
updateRadioOption(4, 40);
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 14
I have tried it this way, which seems to be a good approach, but the step 2 and 4 can’t be passed. I’m asking what might be wrong with my code here. And what might need to be fixed.
Any hints please?
function checkForStraights(arr) {
const newArr = arr.toSorted((a, b) => a - b)
const small = newArr.slice(0, 4);
for(let i = 1; i < newArr.length; i++){
if(small[i] - small[i-1] === 1){
updateRadioOption(3, 30);
}else if(newArr[i] - newArr[i-1] === 1){
updateRadioOption(3, 30);
updateRadioOption(4, 40);
}
updateRadioOption(5, 0);
}
}
you should debug your way of determine if it’s a small straight or a big straight
I have added some console.logs
for(let i = 1; i < newArr.length; i++){
if(small[i] - small[i-1] === 1){
console.log(`comparing ${small[i]} at index ${i} and ${small[i-1]} at index ${i-1}, this is a small straight`)
updateRadioOption(3, 30);
}else if(newArr[i] - newArr[i-1] === 1){
console.log(`comparing ${newArr[i]} at index ${i} and ${newArr[i-1]} at index ${i-1}, this is a large straight`)
updateRadioOption(3, 30);
updateRadioOption(4, 40);
}
[ 1, 1, 2, 3, 4 ]
comparing 2 at index 2 and 1 at index 1, this is a small straight
comparing 3 at index 3 and 2 at index 2, this is a small straight
comparing 4 at index 4 and 3 at index 3, this is a large straight
do you think that these comparisons are determining correctly if this is a small or large straight? it is saying twice that it is a small straight and once that it is a large straight, it seems confused