It’s almost Mother’s Day. This is such a bittersweet holiday for me. On one hand, I like the holiday. I like being a mom and I get to spend the day with the kids. I like hearing them wish me a happy Mother’s day. On the other hand, I’m reminded of my dysfunctional relationship with my mom. I watch so many of my friends get along great with their mom. It’s kind of lonely on that front. I mean, I already lost my dad. It feels like I lost my mom as well. The only thing that kept my mom around was my dad. But having my mom around is so stressful that I know I can’t have her around too much. And when she isn’t having one of her episodes or moods or being narcisstic, she can be pretty fun to hang out with. When my girls need advice or someone to talk to or share something, they come to me. I don’t have a parental figure to go to like that. So this upcoming Mother’s day has me feeling all kinds of feels.
This coffee wired me up today. I only had one cup and one cup is all I will need.
Tommy graduates on Saturday. We are also moving Lexi out of her dorm that day. It will be a fun day. We will have a lot of people to help move Lexi. Normally it is just Tommy, me and Lex. We should go out to dinner for his graduation. Where would he want to go? We need to celebrate. Ok, I know he said the cruise is his present but still. We need to celebrate. Chris is moving out Saturday also. But he is at a different University so his grandparents will have to pick him up.
JavaScript is getting harder. I think. I have a fault of making things harder than they actually are. Somehow the do/while and while loops are harder than for loops. So I’m taking this slow.
————————————————-
A common task in JavaScript is to iterate through the contents of an array. One way to do that is with a for loop. This code will output each element of the array arr to the console:
const arr = [10, 9, 8, 7, 6]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
Remember that arrays have zero-based indexing, which means the last index of the array is length - 1. Our condition for this loop is i < arr.length, which stops the loop when i is equal to length. In this case the last iteration is i === 4 i.e. when i becomes equal to arr.length - 1 and outputs 6 to the console. Then i increases to 5, and the loop terminates because i < arr.length is false. If you have a multi-dimensional array, you can use the same logic as the prior waypoint to loop through both the array and any sub-arrays. Here is an example:
const arr = [ [1, 2], [3, 4], [5, 6] ]; for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr[i].length; j++) { console.log(arr[i][j]); } }
This outputs each sub-element in arr one at a time. Note that for the inner loop, we are checking the .length of arr[i], since arr[i] is itself an array.
The next type of loop you will learn is called a do...while loop. It is called a do...while loop because it will first do one pass of the code inside the loop no matter what, and then continue to run the loop while the specified condition evaluates to true.
const ourArray = []; let i = 0; do { ourArray.push(i); i++; } while (i < 5);
The example above behaves similar to other types of loops, and the resulting array will look like [0, 1, 2, 3, 4]. However, what makes the do...while different from other loops is how it behaves when the condition fails on the first check. Let's see this in action. Here is a regular while loop that will run the code in the loop as long as i < 5:
const ourArray = []; let i = 5; while (i < 5) { ourArray.push(i); i++; }
In this example, we initialize the value of ourArray to an empty array and the value of i to 5. When we execute the while loop, the condition evaluates to false because i is not less than 5, so we do not execute the code inside the loop. The result is that ourArray will end up with no values added to it, and it will still look like [] when all of the code in the example above has completed running. Now, take a look at a do...while loop:
const ourArray = []; let i = 5; do { ourArray.push(i); i++; } while (i < 5);
In this case, we initialize the value of i to 5, just like we did with the while loop. When we get to the next line, there is no condition to evaluate, so we go to the code inside the curly braces and execute it. We will add a single element to the array and then increment i before we get to the condition check. When we finally evaluate the condition i < 5 on the last line, we see that i is now 6, which fails the conditional check, so we exit the loop and are done. At the end of the above example, the value of ourArray is [5]. Essentially, a do...while loop ensures that the code inside the loop will run at least once. Let's try getting a do...while loop to work by pushing values to an array. Recursion is the concept that a function can be expressed in terms of itself. To help understand this, start by thinking about the following task: multiply the first n elements of an array to create the product of those elements. Using a for loop, you could do this:
function multiply(arr, n) { let product = 1; for (let i = 0; i < n; i++) { product *= arr[i]; } return product; }
However, notice that multiply(arr, n) == multiply(arr, n - 1) * arr[n - 1]. That means you can rewrite multiply in terms of itself and never need to use a loop.
function multiply(arr, n) { if (n <= 0) { return 1; } else { return multiply(arr, n - 1) * arr[n - 1]; } }
The recursive version of multiply breaks down like this. In the base case, where n <= 0, it returns 1. For larger values of n, it calls itself, but with n - 1. That function call is evaluated in the same way, calling multiply again until n <= 0. At this point, all the functions can return and the original multiply returns the answer. Note: Recursive functions must have a base case when they return without calling the function again (in this example, when n <= 0), otherwise they can never finish executing.