Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

When the array has three of the same number, your getHighestDuplicates function should update the Three of a Kind radio option with , score = and the total sum of the dice. — this is my error although it works in the preview

const getHighestDuplicates = () => {
  console.log(diceValuesArr);
  let currentNum ;
  let match;
  score = 0;
  let highestMatch = 0;

  for (let x = 0; x < diceValuesArr.length; x++){
    currentNum = diceValuesArr[x];
    match = 0;

    diceValuesArr.forEach((dice) => {
      if (currentNum === dice){
        match++; 
      }

      if (match > highestMatch){
        highestMatch = match;
      }
    });
  }   
  console.log(highestMatch);

  if (highestMatch >= 4) {
      score = sum(diceValuesArr);
      updateRadioOption(1, score);
      console.log('x',highestMatch, currentNum, score);
    } else if (highestMatch === 3){
      score = sum(diceValuesArr);
      updateRadioOption(0, score);
      console.log('f',highestMatch, currentNum, score);
    } else {
      score = 0;
      updateRadioOption(5, score);
    }
};



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

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



// User Editable Region


```css
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

There is a bug in this step. You need to update the last option every time the function is called (not as they said).

Also if you find 4 or more duplicates, both the four of a kind AND the three of a kind button has to be enabled.

thank you.

so here is my update:

if (highestMatch >= 4) {
      score = sum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
      console.log('x',highestMatch, currentNum, score);
    } else if (highestMatch === 3){
      score = sum(diceValuesArr);
      updateRadioOption(0, score);
      console.log('f',highestMatch, currentNum, score);
    } else {
      score = 0;
      updateRadioOption(5, score);
    }

now my error is this:
When the array has less than three of the same number, your getHighestDuplicates function should update the final radio option with , score = 0 .

on your reply: You need to update the last option every time the function is called (not as they said). – can you clarify please.

As I mentioned there is a bug in this step. You need to update the final option all the time, not just when this else is triggered

if (highestMatch >= 4) {
      score = sum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('x',highestMatch, currentNum, score);
    } else if (highestMatch === 3){
      score = sum(diceValuesArr);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('f',highestMatch, currentNum, score);
    } else {
      score = 0;
      updateRadioOption(5, score);
    }

it gives me this warning: When the array has less than three of the same number, your getHighestDuplicates function should not update the first nor the second radio options.

Okay it sounds like your code is updating the first and/or second option at the wrong time?
Have you tried to test?

Edit: it looks like there is a mistake in the logic that checks for the match so I would try to test with an array like [1,1,2,2,3]

i did. here is my console result:
[ 6, 6, 6, 5, 6 ]
4
x 4 6 29

it enabled 3 and 4 and 0 but i still have this warning:
When the array has three of the same number, your getHighestDuplicates function should update the Three of a Kind radio option with , score = and the total sum of the dice.

[ 1, 1, 2, 2, 3 ]
2

only the 0 is enabled for this array.

Because the error is saying something is wrong when duplicates are less than 3, try testing with something that gives 2 duplicates only

Edit: this last error says duplicates is 3 so test with that

here is the 3:
[ 1, 1, 1, 2, 3 ]
3
f 3 3 8

3 and 0 is enabled

here is the error:
When the array has less than three of the same number, your getHighestDuplicates function should not update the first nor the second radio options.

Can you post the code in the function here so I can copy it? (Whatever you have now)

Also make sure to test with 2 duplicates too.

Actually here is another mistake.
You forgot to specify the parameter they wanted.

const getHighestDuplicates = (arr) => {
  console.log(diceValuesArr);
  let currentNum ;
  let match;
  score = 0;
  let highestMatch = 0;
  diceValuesArr = [1,1,1,2,3];  ----hard-coded for now and i disabled rolldice()
  console.log(diceValuesArr);
  for (let x = 0; x < diceValuesArr.length; x++){
    currentNum = diceValuesArr[x];
    match = 0;

    diceValuesArr.forEach((dice) => {
      if (currentNum === dice){
        match++; 
      }

      if (match > highestMatch){
        highestMatch = match;
      }
    });
  }   
  console.log(highestMatch);

  if (highestMatch >= 4) {
      score = sum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('4',highestMatch, currentNum, score);
    } else if (highestMatch === 3){
      score = sum(diceValuesArr);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('3',highestMatch, currentNum, score);
    } else {
      score = 0;
      updateRadioOption(5, score);
    }
};


const sum = (arr) => {
  return arr.reduce((sum, x) => sum += x, 0);
}

i added arr as parameter with a new array:

[ 1, 1, 2, 2, 3 ]
2

0 is enabled and the error:
When the array has three of the same number, your getHighestDuplicates function should update the Three of a Kind radio option with , score = and the total sum of the dice.

Did you remember to update the function call too? Please share the new code.

yes.
updateStats(diceValuesArr);

I think you meant getHighestDuplicates?

Did you remember to use the parameter inside your function?

Please share the new code.

yes.
getHighestDuplicates(diceValuesArr);
[ 1, 6, 2, 4, 3 ]
1

0 is enabled.

still the same: When the array has three of the same number, your getHighestDuplicates function should update the Three of a Kind radio option with , score = and the total sum of the dice.

Please share the new code.

const getHighestDuplicates = (arr) => {
  console.log(diceValuesArr);
  let currentNum ;
  let match;
  score = 0;
  let highestMatch = 0;
  // diceValuesArr = [1,1,2,2,3];
  console.log(diceValuesArr);
  for (let x = 0; x < diceValuesArr.length; x++){
    currentNum = diceValuesArr[x];
    match = 0;

    diceValuesArr.forEach((dice) => {
      if (currentNum === dice){
        match++; 
      }

      if (match > highestMatch){
        highestMatch = match;
      }
    });
  }   
  console.log(highestMatch);

  if (highestMatch >= 4) {
      score = sum(diceValuesArr);
      updateRadioOption(1, score);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('4',highestMatch, currentNum, score);
    } else if (highestMatch === 3){
      score = sum(diceValuesArr);
      updateRadioOption(0, score);
      updateRadioOption(5, 0);
      console.log('3',highestMatch, currentNum, score);
    } else {
      score = 0;
      updateRadioOption(5, score);
    }
};


const sum = (arr) => {
  return arr.reduce((sum, x) => sum += x, 0);
}

rollDiceBtn.addEventListener("click", () => {
  if (rolls === 3) {
    alert("You have made three rolls this round. Please select a score.");
  } else {
    rolls++;
    rollDice();
    updateStats();
    getHighestDuplicates(diceValuesArr);
  }
});