Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

hi, i cannot figure out why my code is not working, i have tried to figure it out with a console.log statement but i dont see which input will make my options 4 and/or 5 enabled… am i missing something ?

Your code so far

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

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

const checkForStraights = (arr) => {
  let sortedArr = arr.toSorted((a,b) => a - b);
  let count = 1;
  for (let i=0; i < sortedArr.length -1; i++){
    if ((sortedArr[i] +1) === sortedArr[i+1]){
      ++count;
    };
    };
   console.log(sortedArr,count);
   if(count === 5){
     updateRadioOption(3, 30);
     updateRadioOption(4, 40);
     return;
   }else if (count === 4){
     updateRadioOption(3, 30);
     return;
   }else if (count <= 3) {
     updateRadioOption(5,0);
     return;
   }
  }

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:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

Welcome to the forum :wave:

In your console you can see this array has a count of 4:
[ 1, 2, 2, 3, 4 ] 4

i am sorry i didnt see the solution here. yes this is a small straight, is it not ? why should the code rejet this input to not be a small straight when it is one ?

the order of the array should’nt be considered, so if [1,2,2,3,4] is passed it should detect 1,2,3,4 ignoring the repeated 2 in the middle. which is a straight of 4 .

heres what the text in the steps said:

A small straight is when four of the dice have consecutive values in any order (Ex. in a roll of 41423, we have 1234) resulting in a score of 30 points. A large straight is when all five dice have consecutive values in any order (Ex. in a roll of 35124, we have 12345) resulting in a score of 40 points.

1 Like

What about this one? [ 1, 2, 3, 5, 6 ] your code identify this as a small straight

1 Like

thank you for pointing this out, this led me to write my code again with a while loop to find the max consecutive numbers with an array having unique values. well thank you for help !!

1 Like

You’re correct, sorry!

1 Like