Tell us what’s happening:
I have no idea why it doesnt pass, help me please(
Your code so far
<!-- file: index.html -->
/* file: script.js */
// User Editable Region
const getHighestDuplicates = () => {
const x = {};
diceValuesArr.forEach(el => {
x[el] = (x[el] || 0) + 1;
});
const xIntoPairs = Object.entries(x);
const numericPairs = xIntoPairs.map(([key, value]) => [Number(key), value]);
const xSorted = numericPairs.sort((a, b) => b[1] - a[1]);
console.log("diceValuesArr:", diceValuesArr);
console.log("x:", x);
console.log("xSorted:", xSorted);
const xMultiplied = xSorted.map(([key, value]) => key * value);
if (xSorted[0][1] === 3){
updateRadioOption(0 ,xMultiplied.reduce((acc, el) => acc + el, 0));
} else if (xSorted[0][1] === 4) {
updateRadioOption(1 ,xMultiplied.reduce((acc, el) => acc + el, 0));
} 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
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 OPR/115.0.0.0
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 7
Teller
February 14, 2025, 2:19am
2
Hi @artemkirpotenko
When the array has less than three of the same number, your getHighestDuplicates
function should update the final radio option with , score = 0
.
The last radio button should show an option with score set to zero, regardless of the outcome of the dice.
Happy coding
const getHighestDuplicates = () => {
const x = {};
diceValuesArr.forEach(el => {
x[el] = (x[el] || 0) + 1;
});
const xIntoPairs = Object.entries(x);
const numericPairs = xIntoPairs.map(([key, value]) => [Number(key), value]);
const xSorted = numericPairs.sort((a, b) => b[1] - a[1]);
console.log("diceValuesArr:", diceValuesArr);
console.log("x:", x);
console.log("xSorted:", xSorted);
const xMultiplied = xSorted.map(([key, value]) => key * value);
if (xSorted[0][1] === 3){
updateRadioOption(0 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} else if (xSorted[0][1] === 4) {
updateRadioOption(1 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} 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();
}
});
still doesnt pass
It says it doesnt pass from 2 to 7
or i guess i start to understand why it doesnt work, should it disable the option if i rolled the dice once more?Because now i can roll 3 times and the result will be captured for all rounds
now i made it like this
const getHighestDuplicates = () => {
const x = {};
diceValuesArr.forEach(el => {
x[el] = (x[el] || 0) + 1;
});
const xIntoPairs = Object.entries(x);
const numericPairs = xIntoPairs.map(([key, value]) => [Number(key), value]);
const xSorted = numericPairs.sort((a, b) => b[1] - a[1]);
console.log("diceValuesArr:", diceValuesArr);
console.log("x:", x);
console.log("xSorted:", xSorted);
scoreInputs.forEach((input, index) => {
input.disabled = true;
input.value = 0;
scoreSpans[index].textContent = "";
});
const xMultiplied = xSorted.map(([key, value]) => key * value);
if (xSorted[0][1] === 3){
updateRadioOption(0 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} else if (xSorted[0][1] === 4) {
updateRadioOption(1 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} 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();
}
});
And still doesnt pass
Teller
February 14, 2025, 11:04pm
6
Four of a kind includes a three of a kind.
const getHighestDuplicates = (arr) => {
const x = {};
diceValuesArr.forEach(el => {
x[el] = (x[el] || 0) + 1;
});
const xIntoPairs = Object.entries(x);
const numericPairs = xIntoPairs.map(([key, value]) => [Number(key), value]);
const xSorted = numericPairs.sort((a, b) => b[1] - a[1]);
const maxCount = xSorted[0][1];
console.log("diceValuesArr:", diceValuesArr);
console.log("x:", x);
console.log("xSorted:", xSorted);
scoreInputs.forEach((input, index) => {
input.disabled = true;
input.value = 0;
scoreSpans[index].textContent = "";
});
const xMultiplied = xSorted.map(([key, value]) => key * value);
if (maxCount === 4) {
updateRadioOption(1 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(0 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} else if (maxCount === 3){
updateRadioOption(0 ,xMultiplied.reduce((acc, el) => acc + el, 0));
updateRadioOption(5 ,0);
} 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(diceValuesArr);
}
});
still no, in console i can see [TypeError: Cannot read properties of undefined (reading ‘1’)], and it doesnt pass from 2 to 7, but the code seems to work
Teller
February 14, 2025, 11:16pm
8
In the getHighestDuplicates
function replace all references to diceValuesArr
with the function parameter.
1 Like
holy, thank you a lot, so stupid mistake
1 Like