Hello, hope everyone is well.
On dice game step 7, I am getting an error "getHighestDuplicates function should update the final radio option with , score = 0
However from what I can tell my solution does update the final radio option with the , score = 0
It seems to work for the scenario of 4 and 3 matching die too.
My solution for this part getHighest is:
const getHighestDuplicates = () => {
let arr = [];
const allDiceTot = diceValuesArr.reduce((acc, el) => {return acc + el}, 0);
const counts = {};
diceValuesArr.forEach((num) => {
counts[num] = (counts[num] || 0) + 1;
})
arr = Object.entries(counts).sort((a, b) => b[1] - a[1]);
console.log(arr);
if (arr[0][1] === 4) {
updateRadioOption(1, allDiceTot);
} else if (arr[0][1] === 3) {
updateRadioOption(0, allDiceTot);
} else {
updateRadioOption(5, 0);
}
};
And my update radio function is:
const updateRadioOption = (index, score) => {
scoreInputs[index].disabled = false;
scoreInputs[index].value = score;
scoreSpans[index].textContent = `, score = ${score}`;
};
Am I missing something obvious? that’s normally the case lol.
please include a link to the problem/challenge/step.
Also you will need to update the last radio button all the time (not just when the duplicates are not found). This is a problem with the step’s description (they don’t ask for this but it is needed to pass the step).
Thanks do you have updated code after reading my last comment?
Tried various was of doing it, doesn’t make any difference so far. Current version is changed to split them out clearly and always call the ‘none’ option.
const getHighestDuplicates = () => {
let arr = [];
const allDiceTot = diceValuesArr.reduce(
(acc, el) => {return acc + el}, 0);
const counts = {};
diceValuesArr.forEach((num) => {
counts[num] = (counts[num] || 0) + 1;
})
arr = Object.entries(counts).sort((a, b) => b[1] - a[1]);
if (arr[0][1] >= 4) {
updateRadioOption(1, allDiceTot);
}
if (arr[0][1] >= 3) {
updateRadioOption(0, allDiceTot);
}
updateRadioOption(5,0);
};
looks like you missed doing part of the requirements:
create a getHighestDuplicates
function which takes an array of numbers.
Thanks. You are right. It works now.
Funnily enough I did start following the spec. and had numbers as the parameter, then called getHighestDuplicates with diceValuesArr.
But while testing and experimenting I figured the diceValuesArr is updated and available in the ‘global scope’ so there’s no need to have this as a parameter at this stage.
But I guess it may be relevant later in the exercise if we call getHighestDuplicates with a different array?
Lesson learned - stick to the requirements… I hadn’t considered it was this, due to the error message being different.
Appreciate the help!
mod edit: code removed so as not to spoil this thread for other readers
honestly your assumption is valid.
The problem (I think) is that the test for this step wants to pass your function different arrays to test your code and wasn’t able to.
I don’t know if the developer could have done something else to test the step but that’s just the way it seems to be at the moment.