Tell us what’s happening:
Passing anything other than " , score = 0" to the else statement throws up an error where the comma is underlined //because there’s no value in the first argument and I can’t put one there// and another error that says "You should have a function named getHighestDuplicates, which I do. Can anyone tell me where this whole thing went wrong?
It’s a little disheartening given that this is literally the best lesson out of the whole Javascript set. I think you should add more of these mini projects where the instructions have multiple steps and really get us to think.
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
function getHighestDuplicates () {
let count = diceValuesArr.reduce((map, val) => {map[value] = (map[val] || 0) + 1});
let totalSum = diceValuesArr.reduce((acc, cur) => acc + cur, 0);
if (count > 3) {
updateRadioOption(1, totalSum);
}
else if (count > 2){
updateRadioOption(0, totalSum);
}
else {
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();
}
});
// User Editable Region
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 7
1 Like
you must be the only one who likes this project! Everyone else complains!
Anyway, right away, I notice a problem because you haven’t implemented the function exactly as required when they said:
create a getHighestDuplicates
function which takes an array of numbers
I’ve tried with an arrow function and gotten the same result. I can switch it and post but again the issue comes down to the last updateRadioOption call arguments. If I type in “(, score = 0)”, as instructed, it claims I don’t have a function at all. I’ve also tried (5,0) which only pushes me back to (, score = 0).
Here’s my arrow function
const getHighestDuplicates = (arr) => {
let count = arr.reduce((map, val) => {map[value] = (map[val] || 0) + 1});
let score = arr.reduce((acc, cur) => acc + cur, 0);
if (count >= 4) {
updateRadioOption(1, score);
}
else if (count >= 3){
updateRadioOption(0, score);
}
else {
updateRadioOption(, score = 0);
}
}
I’ve edited your code for readability. 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 (').
arrow function or not is not the issue.
(defined functions work just as well)
Anyway, (, score = 0)
this is not valid. You can check the console and immediately see a syntax error is posted there.
They said to update the ‘final’ option right? So what index is the ‘final option’ in an array?
The index is 5. I’ve used the following:
updateRadio(5,0),
updateRadio(5, score = 0),
updateRadio(,0),
updateRadio(, score = 0)
I’ve tried just about all I know but if I use initialize the first argument the prompt tells me that my last call should only use (, score = 0) and when I comply the prompt switches to “You should have an function called getHighestDuplicates”
It’s a catch 22 and I don’t know how to get out of it.
You’re misunderstanding the hint.
Just call updateRadioOption with two numbers as per the function definition. One index and one score.
—const getHighestDuplicates = (arr) => {
let count = arr.reduce((map, val) => {map[value] = (map[val] || 0) + 1});
let score = arr.reduce((acc, cur) => acc + cur, 0);
if (count >= 4) {
updateRadioOption(1, score);
}
else if (count >= 3){
updateRadioOption(0, score);
}
else {
updateRadioOption(5, 0);
}
}—
" Sorry, your code does not pass. Hang in there.
When the array has less than three of the same number, your getHighestDuplicates
function should update the final radio option with , score = 0
."
Is it becaue I created a variable named score? Should that matter now that I’m assigning it a different value
Also please check the console as it is saying that value is not defined.
(I believe you meant val)
Keep checking the console as you go for more errors.
also I noticed that this reduce is buggy.
To see some of the issues try to log like this:
console.log(`map: ${map}`);
console.log(`val: ${val}`);
Put these logs inside the reduce callback function and above the first line of code to see what I mean.
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
I rewrote that part of the code using for loops to iterate through the array, creating a variable = to the indexed object etc.
I also found that my if loops logic needed to be reworked so the proper radio buttons were enabled.
I see now that both parameters of the diceValuesArr.reduce() callback are not defined. Would I be wrong in stating that in order to make this code work, I would have to intialize map to be the object of the arr and the val to be something like object[value] = object[value] || 0
You need to initialize it to be a blank object at first (so it can act as accumulator for reduce)
Then you need to return it in your reduce callback.
1 Like
Helo
The issue likely arises because the else statement syntax or function call is incorrect. Ensure the else statement and function call are properly formatted and match the expected function signature.
For feedback on the lesson, consider suggesting more mini projects with multiple steps to the course creators, emphasizing their value in enhancing understanding and critical thinking.
1 Like