Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

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 it like that now, so still not working

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){
        scoreInputs[3].disabled = false;
        updateRadioOption(3, 30);
        scoreInputs[4].disabled = true;
      }else if(newArr[i] - newArr[i-1] === 1){
        updateRadioOption(3, 30);
        updateRadioOption(4, 40);
      }
    updateRadioOption(5, 0);

  }

}

test what your function does with these numbers:

[1,1,2,3,4]
[2,2,3,3,3]
[1,1,1,1,1]

it is failing for these

you should also not do this, use only updateRadioOptions to interact with the radio buttons

Thank you.

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);
  }
}

your function has issues with the same sets of numbers still

let’s take a deeper look:

[1,1,2,3,4]

this is a small straight. if we call checkForStraights([1,1,2,3,4]), we see…

the large straight button should not be selected

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

I got it done. Think you.