A question I got: Are you concerned with other people reading your journal entries?
Well, I don’t write about very private things. I wouldn’t even write those down on paper. I fear someone would read those. There is one that I don’t even know where to start on explaining it. I used to write private things down on paper. Then my mom read my journal and yelled at me about what I wrote about and grounded me. I don’t write about too private things anymore. But what I journal about here, I don’t worry about it.
I kind of wish I kept all my notebooks with my writings in it. I started journaling when I was a kid. I’d either draw or write something. As an adult I use it for my brain health and to write down memories. Especially since my memory isn’t that great. At least when it comes to dates. I can never remember when something happened. With therapy, I need to talk about my past… I can never remember the exact time or timeline when things happened so everything I talk about would be out of order. Not that it really matters in therapy, but it bugs me that I can’t remember the order of when things happen. I have therapy tomorrow. Not sure what I’m going to talk about. I don’t normally go into therapy with a subject in mind.
I started a new medication. This one is for my ADHD, to help me focus. I can’t say it is working yet since I just started. I was initially concerned about it since it is also used for blood pressure and I already have a blood pressure medication. But I talked to the pharmacist about my concerns and I think it will be alright. I’ve been taking it for almost a week so far and things are going smoothly.
JavaScript arrays. I’ve been doing arrays for the past week and this week. Trying to remember everything is making my head spin. This should be in notes but I need an explanation to remember arrays. So after some research:
Arrays in JavaScript are dynamically sized. That’s why you can push() and pop() so easily. In many other languages, such as Java or C++, an array can basically be thought of as a reference to a series of “places” in memory. When you create an array in those languages, you’re essentially saying:
1. Reserve n places in memory
2. Refer to all of those places with the name varName
3. Refer to the xth place of those places as varName[x]
You can certainly remove items from the array, but after the array is “full”, you can’t just keep adding stuff to it.
On the contrary, arrays in JavaScript… are kinda not really arrays. They’re basically just a cardboard label, written in crayon, slapped over basic Objects. In fact, if you do:
typeof [];
In JavaScript, instead of doing that “reserve n places” thing like you do in other languages, you’re basically creating an object where the keys are just numbers. In other words, the following two are sort of equivalent:
const arrArray = ['a','b','c']; const arrObject = { '0': 'a', '1': 'b', '2': 'c' }
I found this picture that shows properties of an array. I saved the picture in case the link goes down one day.
JavaScript notes…
———————————————————–
Remember in the last challenge we mentioned that splice() can take up to three parameters? Well, you can use the third parameter, comprised of one or more element(s), to add to the array. This can be incredibly useful for quickly switching out an element, or a set of elements, for another.
const numbers = [10, 11, 12, 12, 15]; const startIndex = 3; const amountToDelete = 1; numbers.splice(startIndex, amountToDelete, 13, 14); console.log(numbers);
The second occurrence of 12 is removed, and we add 13 and 14 at the same index. The numbers array would now be [ 10, 11, 12, 13, 14, 15 ].
Here, we begin with an array of numbers. Then, we pass the following to splice(): The index at which to begin deleting elements (3), the number of elements to be deleted (1), and the remaining arguments (13, 14) will be inserted starting at that same index. Note that there can be any number of elements (separated by commas) following amountToDelete, each of which gets inserted.
While slice() allows us to be selective about what elements of an array to copy, among several other useful tasks, ES6’s new spread operator allows us to easily copy all of an array’s elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this: …
In practice, we can use the spread operator to copy an array like so:
let thisArray = [true, true, undefined, false, null]; let thatArray = [...thisArray];
thatArray equals [true, true, undefined, false, null]. thisArray remains unchanged and thatArray contains the same elements as thisArray.
Imagine a deck of cards:
Push() – Add a card
Pop() – Take a card off
Peek() – Look at top card
isfull() – Is the whole deck there?
isempty() – Are there no cards on the stack?
Another huge advantage of the spread operator is the ability to combine arrays, or to insert all the elements of one array into another, at any index. With more traditional syntaxes, we can concatenate arrays, but this only allows us to combine arrays at the end of one, and at the start of another. Spread syntax makes the following operation extremely simple:
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme']; let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
thatArray would have the value [‘basil’, ‘cilantro’, ‘sage’, ‘rosemary’, ‘parsley’, ‘thyme’, ‘coriander’].
Using spread syntax, we have just achieved an operation that would have been more complex and more verbose had we used traditional methods.
Since arrays can be changed, or mutated, at any time, there’s no guarantee about where a particular piece of data will be on a given array, or if that element even still exists. Luckily, JavaScript provides us with another built-in method, indexOf(), that allows us to quickly and easily check for the presence of an element on an array. indexOf() takes an element as a parameter, and when called, it returns the position, or index, of that element, or -1 if the element does not exist on the array.
For example:
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears']; fruits.indexOf('dates'); fruits.indexOf('oranges'); fruits.indexOf('pears');
indexOf(‘dates’) returns -1, indexOf(‘oranges’) returns 2, and indexOf(‘pears’) returns 1 (the first index at which each element exists).
Sometimes when working with arrays, it is very handy to be able to iterate through each item to find one or more elements that we might need, or to manipulate an array based on which data items meet a certain set of criteria. JavaScript offers several built in methods that each iterate over arrays in slightly different ways to achieve different results (such as every(), forEach(), map(), etc.), however the technique which is most flexible and offers us the greatest amount of control is a simple for loop.
Consider the following:
function greaterThanTen(arr) { let newArr = []; for (let i = 0; i < arr.length; i++) { if (arr[i] > 10) { newArr.push(arr[i]); } } return newArr; } greaterThanTen([2, 12, 8, 14, 80, 0, 1]);
Using a for loop, this function iterates through and accesses each element of the array, and subjects it to a simple test that we have created. In this way, we have easily and programmatically determined which data items are greater than 10, and returned a new array, [12, 14, 80], containing those items.
One of the most powerful features when thinking of arrays as data structures, is that arrays can contain, or even be completely made up of other arrays. We have seen arrays that contain arrays in previous challenges, but fairly simple ones. However, arrays can contain an infinite depth of arrays that can contain other arrays, each with their own arbitrary levels of depth, and so on. In this way, an array can very quickly become a very complex data structure, known as a multi-dimensional, or nested array. Consider the following example:
let nestedArray = [ ['deep'], [ ['deeper'], ['deeper'] ], [ [ ['deepest'], ['deepest'] ], [ [ ['deepest-est?'] ] ] ] ];
The deep array is nested 2 levels deep. The deeper arrays are 3 levels deep. The deepest arrays are 4 levels, and the deepest-est? is 5.
While this example may seem convoluted, this level of complexity is not unheard of, or even unusual, when dealing with large amounts of data. However, we can still very easily access the deepest levels of an array this complex with bracket notation:
console.log(nestedArray[2][1][0][0][0]);
This logs the string deepest-est?. And now that we know where that piece of data is, we can reset it if we need to:
nestedArray[2][1][0][0][0] = 'deeper still'; console.log(nestedArray[2][1][0][0][0]);
Now it logs deeper still.