Review Algorithmic Thinking by Building a Dice Game - Step 7

Tell us what’s happening:

It is showing the following error, even though I kept the condition for less then 3

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

Your code so far

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

/* file: styles.css */

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

function getHighestDuplicates(arr){
  let count={};
  let score=0
  arr.forEach(num=>{
    count[num]=(count[num]||0)+1;
    score+=num;
  });

  count.forEach(num=>{
    if(n>=4){
      updateRadioOption(1,score);
    }else if(n>=3){
      updateRadioOption(0,score);
    }else if(n<3){
      updateRadioOption(5,0);
    }
      updateRadioOption(5,0);
    
  });
}

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

// User Editable Region

Your browser information:

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

Challenge Information:

Review Algorithmic Thinking by Building a Dice Game - Step 7

You’re on the right way, just missing a few small things.


  • What is n ?

  • count is an object. So, is the forEach method considered an array method or an object method ?

  • The challenge requires:

    Regardless of the outcome, the final option should be updated with a score of 0.

    Then why are you checking for n<3 if you’re doing it anyway at the end ?


arr doesn’t exist in the global scope, it’s defined as a parameter inside the getHighestDuplicates function.

1 Like

I updated it as follows

function getHighestDuplicates(){
  let count={};
  let score=0
  diceValuesArr.forEach(num=>{
    count[num]=(count[num]||0)+1;
    score+=num;
  });

  for(let n in count){
    if(n >= 4){
      updateRadioOption(1, score);
    }else if(n >= 3){
      updateRadioOption(0, score);
    }
    updateRadioOption(5, 0);
  }
}

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

but I am still getting the same error,
When the array has less than three of the same number, your getHighestDuplicates function should update the final radio option with , score = 0 .

The earlier points were really helpfull, thank you.

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 (').

1 Like

create a getHighestDuplicates function which takes an array of numbers.

I am not sure your function takes an array of numbers

any object has key: value pairs, so which of them is n ?

Why did you call the function without any argument ?

1 Like