I feel like cleaning. Organizing. I’m coding at the moment and it is a bit challenging. Probably why I feel like cleaning. It’s a procrastination tactic that my brain wants to do to keep it from doing the challenging thing. My desk needs to be organized. It does. I want to clean out my stuff in the closet. I feel I can minimize my stuff. I haven’t used it in a while, so I can definitely get rid of some stuff. Of course, I will probably feel different when I’m done with coding for today. I just feel an overwhelming urge to clean. Ok, back to coding.
Now that coding is done for the day, I think I will work on my desk. It’s a small post today. I need to get dinner started soon.
JavaScript notes:
—————————————
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. Random numbers are useful for creating random behavior. JavaScript has a Math.random() function that generates a random decimal number between 0 (inclusive) and 1 (exclusive). Thus Math.random() can return a 0 but never return a 1. Note: Like Storing Values with the Assignment Operator, all function calls will be resolved before the return executes, so we can return the value of the Math.random() function. You can generate random decimal numbers with Math.random(), but sometimes you need to generate random whole numbers. The following process will give you a random whole number less than 20: Use Math.random() to generate a random decimal number. Multiply that random decimal number by 20. Use Math.floor() to round this number down to its nearest whole number. Remember that Math.random() can never quite return a 1, so it's impossible to actually get 20 since you are rounding down with Math.floor(). This process will give you a random whole number in the range from 0 to 19.
Math.floor(Math.random() * 20);
You can generate a random whole number in the range from zero to a given number. You can also pick a different lower number for this range.
You'll call your minimum number min and your maximum number max.
This formula gives a random whole number in the range from min to max. Take a moment to read it and try to understand what this code is doing:
Math.floor(Math.random() * (max - min + 1)) + min
The parseInt() function parses a string and returns an integer. Here's an example:
const a = parseInt("007");
Use parseInt() in the convertToInteger function so it converts the input string str into an integer, and returns it.