I have the following error:
“When the array has less than three of the same number, your getHighestDuplicates function should update the final radio option with , score = 0”
Please help to resolve. My code works as expected in preview.
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
function getHighestDuplicates () {
let counts = diceValuesArr.reduce((map, val) => {map[val] = (map[val] || 0) + 1;
let sumOfArr = diceValuesArr.reduce((acc, cur) => acc + cur);
if (val > 3) {
updateRadioOption(0, sumOfArr)
} else if (val === 4) {
updateRadioOption(1, sumOfArr)
} else if (val < 3) {
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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 7
I spoke with a few experienced contributors on this forum and they suggested strongly that you try and write your code with a ‘plain old loop’ rather than a reduce. Your reduce is not returning anything so that’s why there’s an undefined being logged (the first one).
I re-indented the code and added some logs to surface this:
function getHighestDuplicates() {
// 1st arg. of reduce is the callback function
let counts = diceValuesArr.reduce(
(map, val) => {
console.log(`map: ${map}`);
console.log(`val: ${val}`);
console.log(`map[val]: ${map[val]}`);
map[val] = (map[val] || 0) + 1;
let sumOfArr = diceValuesArr.reduce((acc, cur) => acc + cur);
if (val > 3) {
updateRadioOption(0, sumOfArr)
} else if (val === 4) {
updateRadioOption(1, sumOfArr)
} else if (val < 3) {
updateRadioOption(5, 0)
}
},
{} ); //the initial value given to reduce is an empty object
}
Once you have a working for loop that adds up the die then you can feel free to refactor it to a reduce or whatever you want to try next.
Omg, you have even consulted for me! Thank you for your efforts
I tried to follow your recommendations, now the errors have disappeared, but the tests still don’t let me go further. I don’t know why, but the last radio button updates immediately after the first roll, regardless if there are any duplicates or not.
May I ask your help one more time, please?
The new code is below
function getHighestDuplicates() {
let count = {};
for (let i of diceValuesArr) {count[i] ? count[i]++ : (count[i] = 1);
let sumOfArr = diceValuesArr.reduce((acc, cur) => acc + cur, 0);
if (count[i] >= 3) {
updateRadioOption(0, sumOfArr)
} else if (count[i] >= 4) {
updateRadioOption(1, sumOfArr)
} 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();
}
});
It looks like a column of 5 numbers showing how many duplicates has each of 5 dice. Is it wrong? Actually I thought that ELSE word doesn’t not let the code update the radio#6 if there any other radio have been updated, but it lets
I disagree. The instructions don’t seem to imply that we need to update the radio buttons 5 times. They only say we should update them with the sum of the values of all the dice in a single roll?