Review Algorithmic Thinking by Building a Dice Game - Step 14

Tell us what’s happening:

I keep getting the hint :
“If no straight is rolled, your checkForStraights function should not enable the fourth or fifth radio button”
but my radio buttons are only enabled when a straight is rolled …am I missing something?

Your code so far

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

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

const checkForStraights = (arr) => {
  arr.sort((a, b) => a - b);
  let count = 0;
  for (let i = 0; i <= arr.length; i++) {
    if (arr[i] + 1 === arr[i + 1] ||
    arr[-i - 1] === arr[i]) {
      count++;
      if (count === 5) {
        updateRadioOption(3, 30);
        updateRadioOption(4, 40)
      } else if (count === 4) {
        updateRadioOption(3, 30)
      }
    }
  };
  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
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 14

What is this supposed to be doing? Your array is already sorted in ascending order.

Should this code be inside your loop?

sorry for the late reply.
I agree, that little piece really doesn’t seem necessary, I found it when comparing my code to others to see what I was doing wrong, and my code doesn’t recognize the straights without it, though it should if I’m understanding all this correctly. I took my if/else if statement out of the loop and kept that little mirror snippet(since it wont pick up the straights otherwise) and i’m still getting the same hint when I check the code. I’m about to abandon the loop attempt and just compare the diceValuesArr to pre-declared arrays of straights. I’d really like to figure the loop out, but damn javascript can be frustrating :laughing:

So I changed my code to what I mentioned above, and everything is working as it should in the preview, but now its telling me:
" If a small straight is rolled, your checkForStraights function should enable the fourth radio button, set the value to 30 , update the displayed text to , score = 30 and leave the fifth radio button disabled."
I know its gonna end up being something so incredibly simple (and stupid!) when I find it… I’ve had it with this dice game :rofl:
revised code:

  const order = arr.sort((a, b) => a - b).join('');
  const lStr1 = /12345/;
  const lStr2 = /23456/;
  const sStr1 = /1234/;
  const sStr2 = /2345/;
  const sStr3 = /3456/;

  if (lStr1.test(order) || lStr2.test(order)) {
    updateRadioOption(3, 30);
    updateRadioOption(4, 40)
  } else if (sStr1.test(order) || sStr2.test(order) || sStr3.test(order)) {
    updateRadioOption(3, 30)
  };
  updateRadioOption(5, 0)
};```

check what your code does for a set of dice like 1,2,3,3,4, does it work?

I’m getting undefined on the console when I console log checkForStraights([123345]). ok so I need to account for doubles… didn’t think of that

you would need to call console.log(checkForStraights([1,2,3,3,4,5])), with the commas in between

yes, you’re right, it still comes back as undefined, but it makes sense if my regex’s are just the straights themselves and not accounting for doubles being sorted together

also, there are only five dice, so it should be console.log(checkForStraights([2,3,3,4,5]))

1 Like

… this is true. same outcome on the console. would new Set() work for this? and then comparing the set to the regex’s?

not directly, a regex matches on a string, a set is not a string

yeah that would make it an object, hmmm…

here’s what I came up with, still getting undefined on the function console log and newOrder logs as [object Set]. is this moving in the right direction atleast? :

const checkForStraights = (arr) => {
  const order = arr.sort((a, b) => a - b);
  const objOrder = new Set(order);
  const newOrder = objOrder.toString();
  const lStr1 = /12345/;
  const lStr2 = /23456/;
  const sStr1 = /1234/;
  const sStr2 = /2345/;
  const sStr3 = /3456/;

  if (lStr1.test(newOrder) || lStr2.test(newOrder)) {
    updateRadioOption(3, 30);
    updateRadioOption(4, 40)
  } else if (sStr1.test(newOrder) || sStr2.test(newOrder) || sStr3.test(newOrder)) {
    updateRadioOption(3, 30);
  };
  updateRadioOption(5, 0);
};

it’s not the string you want, tho, so it is a string, but you can’t do anything useful with it

you could research how to turn a set into a string

working on that now, actually

forgot about JSON.stringify()… i’m now logging an empty object:

const checkForStraights = (arr) => {
  const order = arr.sort((a, b) => a - b);
  const objOrder = new Set(order);
  const newOrder = JSON.stringify(objOrder);
  const lStr1 = /12345/;
  const lStr2 = /23456/;
  const sStr1 = /1234/;
  const sStr2 = /2345/;
  const sStr3 = /3456/;

  if (lStr1.test(newOrder) || lStr2.test(newOrder)) {
    updateRadioOption(3, 30);
    updateRadioOption(4, 40)
  } else if (sStr1.test(newOrder) || sStr2.test(newOrder) || sStr3.test(newOrder)) {
    updateRadioOption(3, 30);
  };
  updateRadioOption(5, 0);
};

I got it… I gotta organize my notes, this has been staring me in the face for who knows how long:

Array.from(new Set(order)).toString()

great job! you did it yourself!

1 Like

yes :slight_smile: with some much needed hints from you guys, thanks ILM & F.