I’ve had a few therapy appointments so far. So far I’m ok with this new therapist. I don’t feel weird talking about things of my past. With my old therapist, I just felt like I couldn’t really let her into some things. I didn’t know how she would take it. I can’t explain it but I felt like I couldn’t talk about my past much. So I stayed pretty much on the present day. I’ve talked a little bit about my past to the new therapist. Lately we have gotten into the subject on me not feeling comfortable in a conversation and not being able to hold a conversation. Well, not well enough. Without getting into details he has come to the conclusion that the pressure to talk is a trigger for me. When I feel the pressure I blank out and can’t speak. And I put this pressure on myself all the time. So this comes out as me being so quiet all the time. Not sure yet how to combat this. Though I’m not to judge myself after when I do have a conversation with others. How do most people talk so easily? What is your secret?
Sunday, after we move Lexi out of her apartment, Tommy had the idea of coming back here so she can see Karissa and our new puppy, Merlin. I think she will like that.
I accidentally wore my shirt inside-out today. I just changed it around. Not that you needed to know that. Heh.
JavaScript notes…
—————————————
The below is an example of the simplest implementation of an array data structure. This is known as a one-dimensional array, meaning it only has one level, or that it does not have any other arrays nested within it. Notice it contains booleans, strings, and numbers, among other valid JavaScript data types:
let simpleArray = ['one', 2, 'three', true, false, undefined, null]; console.log(simpleArray.length);
The console.log call displays 7.
All arrays have a length property, which as shown above, can be very easily accessed with the syntax Array.length. A more complex implementation of an array can be seen below. This is known as a multi-dimensional array, or an array that contains other arrays. Notice that this array also contains JavaScript objects, which we will examine very closely in our next section, but for now, all you need to know is that arrays are also capable of storing complex objects.
let complexArray = [ [ { one: 1, two: 2 }, { three: 3, four: 4 } ], [ { a: "a", b: "b" }, { c: "c", d: "d" } ] ];
The fundamental feature of any data structure is, of course, the ability to not only store data, but to be able to retrieve that data on command. So, now that we’ve learned how to create an array, let’s begin to think about how we can access that array’s information.
When we define a simple array as seen below, there are 3 items in it:
let ourArray = ["a", "b", "c"];
In an array, each array item has an index. This index doubles as the position of that item in the array, and how you reference it. However, it is important to note, that JavaScript arrays are zero-indexed, meaning that the first element of an array is actually at the zeroth position, not the first. In order to retrieve an element from an array we can enclose an index in brackets and append it to the end of an array, or more commonly, to a variable which references an array object. This is known as bracket notation. For example, if we want to retrieve the a from ourArray and assign it to a variable, we can do so with the following code:
let ourVariable = ourArray[0];
Now ourVariable has the value of a.
In addition to accessing the value associated with an index, you can also set an index to a value using the same notation:
ourArray[1] = "not b anymore";
Using bracket notation, we have now reset the item at index 1 from the string b, to not b anymore. Now ourArray is [“a”, “not b anymore”, “c”].
An array’s length, like the data types it can contain, is not fixed. Arrays can be defined with a length of any number of elements, and elements can be added or removed over time; in other words, arrays are mutable. In this challenge, we will look at two methods with which we can programmatically modify an array: Array.push() and Array.unshift().
Both methods take one or more elements as parameters and add those elements to the array the method is being called on; the push() method adds elements to the end of an array, and unshift() adds elements to the beginning. Consider the following:
let twentyThree = 'XXIII'; let romanNumerals = ['XXI', 'XXII']; romanNumerals.unshift('XIX', 'XX');
romanNumerals would have the value [‘XIX’, ‘XX’, ‘XXI’, ‘XXII’].
romanNumerals.push(twentyThree);
romanNumerals would have the value [‘XIX’, ‘XX’, ‘XXI’, ‘XXII’, ‘XXIII’]. Notice that we can also pass variables, which allows us even greater flexibility in dynamically modifying our array’s data.
Both push() and unshift() have corresponding methods that are nearly functional opposites: pop() and shift(). As you may have guessed by now, instead of adding, pop() removes an element from the end of an array, while shift() removes an element from the beginning. The key difference between pop() and shift() and their cousins push() and unshift(), is that neither method takes parameters, and each only allows an array to be modified by a single element at a time.
Let’s take a look:
let greetings = ['whats up?', 'hello', 'see ya!']; greetings.pop();
greetings would have the value [‘whats up?’, ‘hello’].
greetings.shift();
greetings would have the value [‘hello’].
We can also return the value of the removed element with either method like this:
let popped = greetings.pop();
greetings would have the value [], and popped would have the value hello.
Ok, so we’ve learned how to remove elements from the beginning and end of arrays using shift() and pop(), but what if we want to remove an element from somewhere in the middle? Or remove more than one element at once? Well, that’s where splice() comes in. splice() allows us to do just that: remove any number of consecutive elements from anywhere in an array.
splice() can take up to 3 parameters, but for now, we’ll focus on just the first 2. The first two parameters of splice() are integers which represent indexes, or positions, of items in the array that splice() is being called upon. And remember, arrays are zero-indexed, so to indicate the first element of an array, we would use 0. splice()’s first parameter represents the index on the array from which to begin removing elements, while the second parameter indicates the number of elements to delete. For example:
let array = ['today', 'was', 'not', 'so', 'great']; array.splice(2, 2);
Here we remove 2 elements, beginning with the third element (at index 2). array would have the value [‘today’, ‘was’, ‘great’].
splice() not only modifies the array it’s being called on, but it also returns a new array containing the value of the removed elements:
let array = ['I', 'am', 'feeling', 'really', 'happy']; let newArray = array.splice(3, 2);
newArray has the value [‘really’, ‘happy’].