Review Algorithmic Thinking by Building a Dice Game • Step 14

// JavaScript

const checkForStraights = arr => {
  const regexp5 = /^(12345|23456)$/;
  const regexp4 = /^\d(1234|2345|3456)$|^(1234|2345|3456)\d$/;
  const arrToStr = arr.join('');
  
  if (regexp4.test(arrToStr)) {
    updateRadioOption(3, 30);
  } 
  if (regexp5.test(arrToStr)) {
    updateRadioOption(4, 40);
  } 

  updateRadioOption(5, 0);
}

It works for me (I tested it by myself with various input and playing it) but not for the tests checker. I don’t even pass the second test lol. Any suggestion will be appreciated.

What is the second test? (and instructions?)

Can you provide a link to this step please?

Did you call the function from the correct place? And pass it the correct argument? (We can only see the snippet you posted)

Also, you can hardcode an array in the call to this function to test your code directly (by clicking the roll button)

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/review-algorithmic-thinking-by-building-a-dice-game/step-14

//JavaScript

const checkForStraights = arr => {
  console.log(arr)
  const regexp5= /^(12345|23456)$/;
  const regexp4 = /^\d(1234|2345|3456)$|^(1234|2345|3456)\d$/;
  const arrToStr = arr.join('');
  console.log(arrToStr);
  
  if (regexp4.test(arrToStr)) {
    updateRadioOption(3, 30);
  } 
  if (regexp5.test(arrToStr)) {
    updateRadioOption(4, 40);
  } 

  updateRadioOption(5, 0);
}

//checkForStraights([2, 3, 4, 5, 6])

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);
  }
});
C O N S O L E

// running tests
2. If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30.
3. If a large straight is rolled, your checkForStraights function should enable the fifth radio button, set the value to 40, and update the displayed text to , score = 40.
4. If a large straight is rolled, your checkForStraights function should also enable the fourth radio button, set the value to 30, and update the displayed text to , score = 30.
// tests completed

you don’y need to do this, if you remove the anchors you can just check if the sequence of numbers is present. One thing to consider is if like there are two 2, or two 3… like 12234 this is a good combination, but your code doesn’t validate it

Now, let me see why the tests are rejecting this, if the reason is this one I found or not

Well, it’s not working, the tester is right

image

Ok, you are testing a randomly ordered set of numbers:
If I call console.log(checkForStraights([3, 6, 3, 5, 4])), which contains a straight (3, 4, 5, 6), then arrToStr is 36354 which will not test true for any of the regexes

A small straight is when four of the dice have consecutive values in any order

I think the “in any order” is important here. So 6,5,4,3,5 should also work or 1,3,2,4,6

1 Like

Yes, I see. I didn’t think that “12234” or similars could be a good combination. In fact, with this combination the code doesn’t validate it.

Oh, I see. Thank you so much! I’ll try to fix it with this hint

I added litterally a single .sort() in the code and it passed. Thank you all for the hints!

1 Like

that changes the global variable, use toSorted instead

for the duplicate numbers, even if it’s not tested, you could covert to a set before joining