I feel weird right now. I’m between feeling down and being happy that it is Friday. My sinuses hurt a little, which is why I’m probably feeling down. I’m happy cause it is Friday and I’m also happy that I’m starting to see results from working out. I don’t think I’ve lost inches yet, but I have lost weight. It’s much easier to weigh myself when I’m not on my period.
I sometimes forget that I have sensitive skin. I got a sample for some vitamin C serum and a moisturizer with hylaronic acid and now my face is burning. I have slightly red, dry, rough patches on my face and neck. I think it’s the moisturizer that did it. Maybe the vitamin C. I really should only try things one at a time in case my skin has a reaction. So I’m probably down as well because my face hurts.
I may be getting my Beetle fixed this weekend. I’m excited about that. I can drive around town again. I’m not crazy about driving but I do like to have the option to leave the house if I want to.
I should start thinking of what to have for dinner. Until next time…
JavaScript notes…
——————————————
You are given two arrays and an index.
Copy each element of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain the same after the function runs.
function frankenSplice(arr1, arr2, n) { let result = []; result.push(...arr2.slice(0, n)); result.push(...arr1); result.push(...arr2.slice(n, arr2.length)); return result; } console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
Remove all falsy values from an array. Return a new array; do not mutate the original array.
Falsy values in JavaScript are false, null, 0, “”, undefined, and NaN.
Hint: Try converting each value to a Boolean.
function bouncer(arr) { let result = []; let i = result; for (i = 0; i < arr.length; i += 1) { if (arr[i]) { result.push(arr[i]); } } return result; } console.log(bouncer([7, "ate", "", false, 9]));
Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.
For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).
Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).
function getIndexToIns(arr, num) { let sortedArray = arr.sort(function (a, b) {return a - b}); for (let i = 0; i < sortedArray.length; i += 1) { if (num > arr[i - 1] && num <= arr[i]) { return i; } } if (sortedArray[sortedArray.length - 1] < num) { return sortedArray.length; } return 0; } console.log(getIndexToIns([40, 60], 50));
Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string hello does not contain a y.
Lastly, ["Alien", "line"], should return true because all of the letters in line are present in Alien.
function mutation(arr) { let firstElement = arr[0].toLowerCase(); let secondElement = arr[1].toLowerCase(); for (let i = 0; i < secondElement.length; i += 1) { let letter = secondElement[i].toLowerCase(); if (firstElement.indexOf(letter) === -1) { return false; } } return true; } console.log(mutation(["Mary", "Army"]));
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
function chunkArrayInGroups(arr, size) { let result = []; for (let i = 0; i < arr.length; i += size) { result.push(arr.slice(i, i + size)) } return result; } console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2));
Think about things people see every day, like cars, shops, and birds. These are all objects: tangible things people can observe and interact with.
What are some qualities of these objects? A car has wheels. Shops sell items. Birds have wings.
These qualities, or properties, define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties. For example, all cars have wheels, but not all cars have the same number of wheels.
Objects in JavaScript are used to model real-world objects, giving them properties and behavior just like their real-world counterparts. Here's an example using these concepts to create a duck object:
let duck = { name: "Aflac", numLegs: 2 };
This duck object has two property/value pairs: a name of Aflac and a numLegs of 2.
let dog = { name: "Merlin", numLegs: 4 };
The last challenge created an object with various properties. Now you'll see how to access the values of those properties. Here's an example:
let duck = { name: "Aflac", numLegs: 2 }; console.log(duck.name);
Dot notation is used on the object name, duck, followed by the name of the property, name, to access the value of Aflac.
Print both properties of the dog object to your console.
let dog = { name: "Spot", numLegs: 4 }; // Only change code below this line console.log(dog.name, dog.numLegs);
Objects can have a special type of property, called a method.
Methods are properties that are functions. This adds different behavior to an object. Here is the duck example with a method:
let duck = { name: "Aflac", numLegs: 2, sayName: function() {return "The name of this duck is " + duck.name + ".";} }; duck.sayName();
The example adds the sayName method, which is a function that returns a sentence giving the name of the duck. Notice that the method accessed the name property in the return statement using duck.name. The next challenge will cover another way to do this.
Using the dog object, give it a method called sayLegs. The method should return the sentence This dog has 4 legs.
dog.sayLegs() should be a function.
Waiting:dog.sayLegs() should return the given string - note that punctuation and spacing matter.
let dog = { name: "Spot", numLegs: 4, }; dog.sayLegs();
let dog = { name: "Spot", numLegs: 4, sayLegs: function() { return "This dog has " + dog.numLegs + " legs."; } }; dog.sayLegs();
The last challenge introduced a method to the duck object. It used duck.name dot notation to access the value for the name property within the return statement:
sayName: function() {return "The name of this duck is " + duck.name + ".";}
While this is a valid way to access the object's property, there is a pitfall here. If the variable name changes, any code referencing the original name would need to be updated as well. In a short object definition, it isn't a problem, but if an object has many references to its properties there is a greater chance for error.
A way to avoid these issues is with the this keyword:
let duck = { name: "Aflac", numLegs: 2, sayName: function() {return "The name of this duck is " + this.name + ".";} };
this is a deep topic, and the above example is only one way to use it. In the current context, this refers to the object that the method is associated with: duck. If the object's name is changed to mallard, it is not necessary to find all the references to duck in the code. It makes the code reusable and easier to read.
Modify the dog.sayLegs method to remove any references to dog. Use the duck example for guidance.
let dog = { name: "Spot", numLegs: 4, sayLegs: function() {return "This dog has " + this.numLegs + " legs.";} }; dog.sayLegs();
Constructors are functions that create new objects. They define properties and behaviors that will belong to the new object. Think of them as a blueprint for the creation of new objects.
Here is an example of a constructor:
function Bird() { this.name = "Albert"; this.color = "blue"; this.numLegs = 2; }
This constructor defines a Bird object with properties name, color, and numLegs set to Albert, blue, and 2, respectively. Constructors follow a few conventions:
Constructors are defined with a capitalized name to distinguish them from other functions that are not constructors.
Constructors use the keyword this to set properties of the object they will create. Inside the constructor, this refers to the new object it will create.
Constructors define properties and behaviors instead of returning a value as other functions might.
Create a constructor, Dog, with properties name, color, and numLegs that are set to a string, a string, and a number, respectively.
function Dog() { this.name = "Merlin"; this.color = "brown"; this.numLegs = 4; }
Here's the Bird constructor from the previous challenge:
function Bird() { this.name = "Albert"; this.color = "blue"; this.numLegs = 2; } let blueBird = new Bird();
NOTE: this inside the constructor always refers to the object being created.
Notice that the new operator is used when calling a constructor. This tells JavaScript to create a new instance of Bird called blueBird. Without the new operator, this inside the constructor would not point to the newly created object, giving unexpected results. Now blueBird has all the properties defined inside the Bird constructor:
blueBird.name; blueBird.color; blueBird.numLegs;
Just like any other object, its properties can be accessed and modified:
blueBird.name = 'Elvira'; blueBird.name;
Use the Dog constructor from the last lesson to create a new instance of Dog, assigning it to a variable hound.
function Dog() { this.name = "Rupert"; this.color = "brown"; this.numLegs = 4; } // Only change code below this line
function Dog() { this.name = "Rupert"; this.color = "brown"; this.numLegs = 4; } // Only change code below this line let hound = new Dog();