This morning I went to get gas in my car. And my engine light came on while my car was revving while idle. It was so weird. I came home and told Kel what was happening, and she asked if Alex had put my oil cap back on.. cause that was the only thing we could think of. Alex put oil in my car yesterday. We went and checked under the hood, and sure enough, the oil cap was not there. Luckily we found it in the driveway. We put it back on and started the car. The car stopped revving while it is in idle. I must drive it later to take Alex to the barber shop, Walmart, and the post office. I hope the missing oil cap was the problem and not anything else. I’m also thinking of buying a three-hole punch. I spent an hour looking for one last night, and ours seems to have disappeared. Oh, and a bigger bottle of Naproxin since I was asked this morning why I bought such a small bottle.
It’s warming up outside. It’s so nice. We haven’t needed to use the pellet stove and are preparing the swamp cooler for warmer weather. I tend to turn down the swamp when I’m home alone. I don’t care for the house to be freezing. I think Karissa and I are the only ones that feel this way.
Yesterday and Monday, I did some errands. The bank and post office, and I made street tacos and Spanish rice. It’s been a pretty good week so far.
JavaScript this week has been about arrays and functions. I’m not going to lie; I’ve been a little confused and slowly understanding them. Lots of Googling. Here are my notes:
—————————————————-
Bracket notation is a way to get a character at a specific index within a string.
Most modern programming languages, like JavaScript, don’t start counting at 1 like humans do. They start at 0. This is referred to as Zero-based indexing.
For example, the character at index 0 in the word Charles is C. So if const firstName = “Charles”, you can get the value of the first letter of the string by using firstName[0].
const firstName = "Charles"; const firstLetter = firstName[0];
In JavaScript, String values are immutable, which means that they cannot be altered once created.
let myStr = "Bob"; myStr[0] = "J";
For example, the following code will produce an error because the letter B in the string Bob cannot be changed to the letter J
Note that this does not mean that myStr could not be re-assigned. The only way to change myStr would be to assign it with a new value, like this:
let myStr = "Bob"; myStr = "Job";
In order to get the last letter of a string, you can subtract one from the string’s length.
For example, if const firstName = “Ada”, you can get the value of the last letter of the string by using firstName[firstName.length – 1].
Example:
const firstName = "Ada"; const lastLetter = firstName[firstName.length - 1];
With JavaScript array variables, we can store several pieces of data in one place.
You start an array declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:
const sandwich = ["peanut butter", "jelly", "bread"];
Unlike strings, the entries of arrays are mutable and can be changed freely, even if the array was declared with const.
const ourArray = [50, 40, 30]; ourArray[0] = 15;
ourArray now has the value [15, 40, 30].
One way to think of a multi-dimensional array, is as an array of arrays. When you use brackets to access your array, the first set of brackets refers to the entries in the outermost (the first level) array, and each additional pair of brackets refers to the next level of entries inside.
const arr = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [[10, 11, 12], 13, 14] ]; const subarray = arr[3]; const nestedSubarray = arr[3][0]; const element = arr[3][0][1];
An easy way to append data to the end of an array is via the push() function.
.push() takes one or more parameters and “pushes” them onto the end of the array.
const arr1 = [1, 2, 3]; arr1.push(4); const arr2 = ["Stimpson", "J", "cat"]; arr2.push(["happy", "joy"]);
arr1 now has the value [1, 2, 3, 4] and arr2 has the value [“Stimpson”, “J”, “cat”, [“happy”, “joy”]].
Another way to change the data in an array is with the .pop() function.
.pop() is used to pop a value off of the end of an array. We can store this popped off value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element.
Any type of entry can be popped off of an array – numbers, strings, even nested arrays.
const threeArr = [1, 4, 6]; const oneDown = threeArr.pop(); console.log(oneDown); console.log(threeArr);
pop() always removes the last element of an array. What if you want to remove the first?
That’s where .shift() comes in. It works just like .pop(), except it removes the first element instead of the last.
const ourArray = ["Stimpson", "J", ["cat"]]; const removedFromOurArray = ourArray.shift();
Not only can you shift elements off of the beginning of an array, you can also unshift elements to the beginning of an array i.e. add elements in front of the array.
.unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array.
const ourArray = ["Stimpson", "J", "cat"]; ourArray.shift(); ourArray.unshift("Happy");
In JavaScript, we can divide up our code into reusable parts called functions.
function functionName() { console.log("Hello World"); }
Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.
function testFun(param1, param2) { console.log(param1, param2); }
We can pass values into a function with arguments. You can use a return statement to send a value back out of a function.
function plusThree(num) { return num + 3; } const answer = plusThree(5);
In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.
Variables which are declared without the let or const keywords are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with let or const.
Variables which are declared within a function, as well as the function parameters, have local scope. That means they are only visible within that function.
function myTest() { const loc = "foo"; console.log(loc); } myTest(); console.log(loc);
It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.
const someVar = "Hat"; function myFun() { const someVar = "Head"; return someVar; }