Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I’ve already tested my function and found nothing wrong with it; could you tell me where I’m wrong?

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

const checkForStraights = (dice) => {
  const diceUnique = [...new Set(dice.sort((a, b) => a - b))];
  console.log(diceUnique)
  if (diceUnique.length === 5) {
    updateRadioOption(4, 40);
    return;
  } 
  let isSmallStraight = true;
  if(diceUnique.length === 4) {
      for (let i = 0; i <= 2; i++) {
        if(diceUnique[i] !== diceUnique[i+1] - 1) {
          isSmallStraight = false
          return;
        }
      }
    } else isSmallStraight = false;
  
  if(isSmallStraight) {
    updateRadioOption(3, 30);
    return;
  }

  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/127.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

/* file: script.js */
const checkForStraights = (dice) => {
  let isLargeStraight = true;
  let isSmallStraight = true;

  const diceUnique = [...new Set(dice.sort((a, b) => a - b))];

  if (diceUnique.length === 5) {
      for (let i = 0; i <= 3; i++) {
        if(diceUnique[i] !== diceUnique[i+1] - 1) {
          isLargeStraight = false
          return;
        }
      }
    } else isLargeStraight = false;


  if(diceUnique.length === 4) {
      for (let i = 0; i <= 2; i++) {
        if(diceUnique[i] !== diceUnique[i+1] - 1) {
          isSmallStraight = false
          return;
        }
      }
    } else isSmallStraight = false;
  
  if(isSmallStraight) {
    updateRadioOption(3, 30);
    return;
  }
  if (isLargeStraight) {
    updateRadioOption(4, 40);
    return;
  }

  updateRadioOption(5, 0); 
}

I’ve fix some of case but it still wrong

The solution for this is not to easy I guess, after looking online I can tell you there wasn’t one else or else if statements in the function. Good luck

1 Like