Review Algorithmic Thinking by Building a Dice Game - Step 15

need help on this problem, idk what to do here, i have tired everything i can and still dont know how to solve this, pls help me

const getHighestDuplicates = (arr) => {
  const counts = {};

  for (const num of arr) {
    if (counts[num]) {
      counts[num]++;
    } else {
      counts[num] = 1;
    }
  }

  let highestCount = 0;

  for (const num of arr) {
    const count = counts[num];
    if (count >= 3 && count > highestCount) {
      highestCount = count;
    }
    if (count >= 4 && count > highestCount) {
      highestCount = count;
    }
  }

  const sumOfAllDice = arr.reduce((a, b) => a + b, 0);

  if (highestCount >= 4) {
    updateRadioOption(1, sumOfAllDice);
  }

  if (highestCount >= 3) {
    updateRadioOption(0, sumOfAllDice);
  }

  updateRadioOption(5, 0);
};

const detectFullHouse = (arr) => {
  const counts = {};

  for (const num of arr) {
    counts[num] = counts[num] ? counts[num] + 1 : 1;
  }

  const hasThreeOfAKind = Object.values(counts).includes(3);
  const hasPair = Object.values(counts).includes(2);

  if (hasThreeOfAKind && hasPair) {
    updateRadioOption(2, 25);
  }

  updateRadioOption(5, 0);
};

const checkForStraights = (arr) => {
  const sortedNumbersArr = arr.sort((a, b) => a - b);
  const uniqueNumbersArr = [...new Set(sortedNumbersArr)];
  const uniqueNumbersStr = uniqueNumbersArr.join("");

  const smallStraightsArr = ["1234", "2345", "3456"];
  const largeStraightsArr = ["12345", "23456"];

  if (smallStraightsArr.some(straight => uniqueNumbersStr.includes(straight))) {
    updateRadioOption(3, 30);
  }

  if (largeStraightsArr.includes(uniqueNumbersStr)) {
    updateRadioOption(4, 40);
  }

  updateRadioOption(5, 0);
};

const resetRadioOptions = () => {
  scoreInputs.forEach((input) => {
    input.disabled = true;
    input.checked = false;
  });

  scoreSpans.forEach((span) => {
    span.textContent = "";
  });
};

const resetGame = () => {
  diceValuesArr = [0, 0, 0, 0, 0];
  score = 0;
  round = 1;
  rolls = 0;

  listOfAllDice.forEach((dice, index) => {
    dice.textContent = diceValuesArr[index];
  });

  totalScoreElement.textContent = score;
  scoreHistory.innerHTML = "";

  rollsElement.textContent = rolls;
  roundElement.textContent = round;

  resetRadioOptions();
};

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

please share a link to the challenge

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

You might notice that your code enabled the last radio button (“None of the Above”) multiple times within your getHighestDuplicates , detectFullHouse and checkForStraights functions.

Can you identify the line of code the instructions are referring to?

yes i can, but i still dont understand, can you show me how to do it step by step?

Please show the line of code the instructions are referring to.

this is the line of code the instructions are referring .

Lastly, you should tidy up your code with a bit of refactoring! You might notice that your code enabled the last radio button (“None of the Above”) multiple times within your getHighestDuplicates , detectFullHouse and checkForStraights functions. Refactor your code so that instead of enabling the last radio button at the end of each function, it should only be done once inside of rollDiceBtn .

That’s not a line of code, those are the instructions.

My apologies, thats the problem I really have no idea how to do this step, im lost

your code enabled the last radio button (“None of the Above”) multiple times within your getHighestDuplicates , detectFullHouse and checkForStraights functions.

So there is a line of code which enables the last radio button. The same line of code is in all three of the mentioned functions.

Look for one line of code which:

  • acts on radio buttons
  • enables the last radio button
  • appears in each of those functions.

Even if it’s not correct just reply with your best guess, but make sure it’s a line of code, and we can take it from there?

oh ok i understand, so is this the line of code?

Your getHighestDuplicates function should not enable the final radio button, or set the final span text, regardless of if duplicates are rolled.

Those are instructions.

A line of code would be

 for (const num of arr) {
    if (counts[num]) {
      counts[num]++;
    } else {
      counts[num] = 1;
    }
  }

or

 counts[num] = counts[num] ? counts[num] + 1 : 1;

That’s all the code, can you focus it down to a single line of code that relates to radio buttons?

This is a crucial skill, you’ll need to try.

I’ve removed the code you posted because it’s not what I asked for and it’s just adding noise to the conversation.

you have individuated a line now, what are you asked to do with it?

why did you delete that post?

because idk if its right or wrong, also i really dont know what it wants from me

you have posted the instructions multiple times, what is your best interpretation of them?

How does deleting the line of code help? It was correct, btw.

1 Like