I had a quiet weekend. Tommy is still feeling unwell, so we stayed home. We planned to have chicken fajitas but changed our minds and ordered pizza instead. Kel and I had to wait for our pizza for about an hour due to a mix-up with our order, but everything turned out okay.
Gosh, April is already over. This month went by fast. I’m excited for the weather to warm up in May, even though it’s windy today and only getting up to 67 degrees.
Today’s coding session went well. I struggle with finding topics to write about regarding coding. Still, we covered a lot of loops today, including if, else-if, and for-of loops. Surprisingly, I managed to get most of them right without any assistance.
I just finished reading the book ‘Isles of the Gods,’ which took me quite some time to complete. Due to coding, I only had a little free time to read during the weekdays, and on weekends, I wasn’t always in the mood to read. The book was exciting but had five different viewpoints, making remembering all the characters and their backgrounds challenging while switching between the various perspectives.
At the moment, I am delving into Safe People, which my therapist suggested. Usually, self-help books don’t pique my interest, but I’m willing to try to see if this book can offer assistance. This particular book incorporates a religious perspective, which may be acceptable. Nonetheless, it is somewhat peculiar.
I think I’m coming down with a cold, or it could be allergies. But I have Fall allergies and not really Spring allergies. But I guess it can be possible. I have a headache and my throat hurts a bit. I hope I’m not getting sick and this is just allergies and I will feel better soon.
Alexis just called me. She is writing a kid’s play and the play is about deafness. She wanted to know how it was like for me as a kid. I told her how I use to bring a tape recorder into class and secretly record the teachers so I can replay it back at home where I can hear what they are saying. I relied a lot on written instructions rather than verbal. I told her that her sister is probably a better person to ask since Karissa is deaf in one ear and only has 20% of her right ear.
JavaScript notes…
——————————–
FreeCodeCamp Dice Game steps 26 – 45
Step 26
To see the round and rolls values update on the screen, call your updateStats function inside the else clause of the rollDiceBtn event listener.
Now you should be able to roll the dice a few times and see the values update.
updateStats()
Step 27
Each time you roll the dice, you could end up with a “Three of a kind”, “Four of a kind”, “Full house”, “Straight” or a random combination of numbers. Based on the outcome, you can make a selection and add points to your score.
Start by creating an arrow function called updateRadioOption that takes optionNode and score as parameters.
const updateRadioOption = (optionNode, score) => { }
Step 28
To enable the radio buttons, you should set the disabled property on scoreInputs[optionNode] to false.
const updateRadioOption = (optionNode, score) => { scoreInputs[optionNode].disabled = false; };
Step 29
Next, you will need to update the radio button’s value to the current score.
const updateRadioOption = (optionNode, score) => { scoreInputs[optionNode].disabled = false; scoreInputs[optionNode].value = score; };
Step 30
To display the current score, update the text content for the span element next to the radio button to be the following template literal: , score = ${score}.
const updateRadioOption = (optionNode, score) => { scoreInputs[optionNode].disabled = false; scoreInputs[optionNode].value = score; scoreSpans[optionNode].textContent = `, score = ${score}` };
Step 31
To test out selecting a radio button option, call the updateRadioOption function and pass in the numbers 0 and 10 for the arguments.
Roll the dice again and you should see that the first radio button is enabled and displays a current score of 10.
updateRadioOption(0, 10)
Step 32
Now that you have verified the updateRadioOption function works, remove the function call from your else clause.
(remove updateRadioOption function)
Step 33
If you roll the dice and get a “Three of a kind” or “Four of a kind”, then you can get a score totalling the sum of all five dice values. For the next few steps, you will build out an algorithm that tracks any duplicates found in the diceValuesArr and displays a score next to the first two radio buttons.
Start by creating an arrow function called getHighestDuplicates that has a parameter called arr.
const getHighestDuplicates = (arr) => { }
Step 34
To detect if a roll result is a “Three of a kind” or a “Four of a kind”, you will need to count the number of occurrences for each unique number in the arr.
Inside your getHighestDuplicates function, create a const variable called counts and assign it an empty object.
const getHighestDuplicates = (arr) => { const counts = {} };
Step 35
Below your counts variable, create a for…of loop that will iterate through your arr. The variable name for the for…of loop should be called num.
const getHighestDuplicates = (arr) => { const counts = {}; for (const num of arr) { } };
Step 36
For each iteration in the arr, you will need to check if the current number exists in the counts object.
Inside your for…of loop, add an if statement to check if the num key exists in the counts object. If so, increment the value of counts[num] by 1.
Below your if statement, include an else clause that assigns 1 to counts[num] for the first occurrence of that number in the counts object.
if (counts[num]) { counts[num]++; } else { counts[num] = 1; }
Step 37
The next step is to keep track of when a particular number appears three or four times within the arr.
Use let to create a highestCount variable and assign it the value of 0.
let highestCount = 0;
Step 38
Next, create another for…of loop that loops through the arr. The variable name for the for…of loop should be called num.
for (const num of arr) { }
Step 39
For each iteration of the loop, you will need to get the current duplicate count for each number in the arr.
Create a const variable called count and assign it the value of counts[num].
for (const num of arr) { const count = counts[num] }
Step 40
To detect if the dice roll produced a “Three of a kind”, you will need to create an if statement to check if the current count is greater than or equal to 3 and greater than the current highest count. If so, assign count to the highestCount variable.
for (const num of arr) { const count = counts[num]; if (count >= 3 && count > highestCount) { highestCount = count; } }
Step 41
To detect if the dice roll produced a “Four of a kind”, you will need to create an if statement to check if the current count is greater than or equal to 4 and greater than the current highest count. If so, assign count to the highestCount variable.
if (count >= 4 && count > highestCount) { highestCount = count; }
Step 42
If the user rolls a “Three of a kind” or “Four of a kind”, then they will receive a score totalling the sum of all five dice values.
Use the reduce method on diceValuesArr. For the callback function, use a and b as parameters. Within the callback function, return the sum of a and b. For the initial value, use the number 0.
Finally, assign the entire result to a const variable called sumOfAllDice.
const sumOfAllDice = diceValuesArr.reduce((a, b) => a + b, 0);
Step 43
If the highest number of duplicates is 4 or more, then you will want to enable the radio button for the “Four of a kind” option and display the score next to it.
Add an if statement that checks if highestCount is greater than or equal to 4. If so, call the updateRadioOption and pass in the number 1 and sumOfAllDice for the arguments.
if (highestCount >= 4) { updateRadioOption(1, sumOfAllDice); }
Step 44
If the highest number of duplicates is 3 or more, then you will want to enable the radio button for the “Three of a kind” option and display the score next to it.
Add an if statement that checks if highestCount is greater than or equal to 3. If so, call the updateRadioOption and pass in the number 0 and sumOfAllDice for the arguments.
if (highestCount >= 3) { updateRadioOption(0, sumOfAllDice); }
Step 45
If the user does not get a “Three of a kind” or “Four of kind”, then they will not receive any points for that round.
At the end of your getHighestDuplicates function, call the updateRadioOption function with the arguments of 5 and 0.
updateRadioOption(5, 0);