Tell us what’s happening:
My sum just sums up all numbers.
Trying to figure out right direction to make it sum only numbers that are duplicated 3 or more times.
Also im not passing 4,6 and 7 steps
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
// User Editable Region
const getHighestDuplicates = () => {
let count = {};
diceValuesArr.forEach((num) => {
if(count[num]) {
count[num]++
} else {
count[num] = 1;
}
});
const sum = diceValuesArr.reduce((a, b) => a + b, 0);
if (count >= 4) {
updateRadioOption(1, 0, `score = ${sum}`);
} else if (count >= 3) {
updateRadioOption(0, `score = ${sum}`);
} else {
updateRadioOption( 5,score = 0);
}
console.log(sum);
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Review Algorithmic Thinking by Building a Dice Game - Step 7
You will need to pass one at a time so if four is failing you should pass that first. Are you passing all except 4,6, and 7?
Yeah im passing all but 4,6 and 7
Hi there!
Use a parameter, instead of actual array.
DexaPoli:
getHighestDuplicates();
Then add the actual array diceValuesArr
within the function call in eventListener callback function.
diceValuesArr.forEach((num) => {
if(count[num]) {
count[num]++
} else {
count[num] = 1;
}
});
const sum = num.reduce((a, b) => a + b, 0);
Like this?
getHighestDuplicates(diceValuesArr);
This is just for reference that i also did that
Don’t use diceValuesArr
for calculating the logic within the function getHeighestDuplicate
. Instead use a parameter.
Add a parameter to the function and use it to build the logic within the function.
const getHighestDuplicates = (arr) => {
let count = {};
let totalScore = 0;
arr.forEach((num) => {
if(count[num]) {
count[num]++
} else {
count[num] = 1;
}
});
const sum = arr.reduce((a, b) => a + b, 0);
totalScore += sum;
if (count >= 3) {
updateRadioOption(0, totalScore);
} else if (count >= 4) {
updateRadioOption(1, totalScore);
} else {
updateRadioOption(5, 0);
}
console.log(totalScore);
console.log(count);
}
rollDiceBtn.addEventListener("click", () => {
if (rolls === 30) {
alert("You have made three rolls this round. Please select a score.");
} else {
rolls++;
rollDice();
updateStats();
getHighestDuplicates(diceValuesArr);
}
});
I think am making progress. in console im getting sum of numbers and count that counts of numbers in array, but when i get 3+ of same number radio still doesnt update.
rolls is at 30 so i can do it more than 3 times
updateRadioOption(5, 0);
even when i update last one and instead 0 put totalScore it will count it and display but not for other 2 so im very confused now why is that
Teller
December 26, 2024, 8:32pm
9
Hi @DexaPoli
What does the console log for count
display?
Happy coding
13
{ ‘1’: 1, ‘2’: 1, ‘3’: 2, ‘4’: 1 }
14
{ ‘2’: 3, ‘3’: 1, ‘5’: 1 }
18
{ ‘2’: 1, ‘3’: 1, ‘4’: 2, ‘5’: 1 }
Teller
December 27, 2024, 1:07am
11
What will happen when an object is greater than or equal to an integer?
DexaPoli:
if (count >= 3) {
updateRadioOption(0, totalScore);
It should do this but it doesnt
Teller
December 27, 2024, 1:26am
13
if ({ '3': 3, '4': 2, '5': 1 } >= 3) {
console.log("true");
}
else {
console.log("false");
}
The if
statement will never execute.