It seems that the stomach flu is making its rounds, and I’m thankful to have avoided it so far. Kel, Alex, and Tommy all fell ill with it last weekend. On Saturday, Tommy and I went to lunch while we waited for Kel’s dad to be released from the hospital. Afterward, we stopped by Costco to pick up a few items and collect the cake. When the cake was brought out, it was a white half-sheet cake. I had specifically requested a round chocolate cake, which was noted on their order form. Fortunately, they had a chocolate cake in the back and decorated it while we waited. After Costco, we made our way to the hospital to bring Kel’s dad home, stopping at Walgreens for his medication along the way. We had Lexi’s birthday party on Saturday night with pizza and cake.
By the way, when I had my hearing aids cleaned, the employee pointed out that there was blood on my right hearing aid. I’m aware that I have an ear infection, and I was hoping it would resolve on its own. I’ve scheduled a doctor’s appointment for Thursday morning.
Tomorrow, Kel and I are going to a friend’s house for lunch. This means I need to find a suitable location for my therapy appointment at 1:30. Alex has a job interview tomorrow, so he will be joining Kel and me when we go out.
In coding, I’m trying to find the missing number in an array. From last week…
function findMissingNumber(arr) { // Code here }
To further break it down and to help myself since I easily get confused with math:
Let’s say our input array is:
arr = [1, 2, 4, 5, 6];
Step 1: Determine n
Since one number is missing, the full sequence should have n = arr.length + 1 = 6 numbers.
Step 2: Calculate Expected Sum
Using the formula: expectedSum = 6(6+1)/2 = 6*7/2 = 21
Step 3: Calculate Actual Sum
Sum of the given numbers: 1+2+4+5+6=18
Step 4: Find the Missing Number
missingNumber = 21−18=3 the missing number is 3.
We come out with the code looking like this:
function findMissingNumber(arr) { let n = arr.length + 1; // The expected length (including the missing number) let expectedSum = (n * (n + 1)) / 2; // Sum of first n numbers let actualSum = arr.reduce((sum, num) => sum + num, 0); // Sum of array elements return expectedSum - actualSum; // Missing number }
To calculate the actual sum, we utilize the `reduce` method. The syntax for the `reduce` method is as follows:
array.reduce(callbackFunction, initialValue)
In our code, the `reduce` method is implemented like this:
let actualSum = arr.reduce((sum, num) => sum + num, 0);
Here, `sum` (the accumulator) starts at 0, which serves as the initial value. `num` represents the current value, corresponding to each number in the array. The function increments `sum` by `num` in each iteration. Ultimately, this code computes the total of all the numbers in the array.
I’m not sure if I made that more confusing. The next function I’m working on is to group an array of objects by a specified property. The skeleton code looks like this:
function groupBy(arr, key) { // Code here }
From my research, it seems that we will use the `.reduce()` method to efficiently group the objects. At this point, I’m going to take a break and make myself some matcha tea. As I look at my keyboard, I realize it needs a thorough clean. Sandy sneezed on it a few times, and while I wiped it down, it definitely requires more attention.





