We want to further our thinking of functions as expressions that can be stored in variables - just like other data types!
First-class objects : a fancy term that indicates we can treat functions as “normal” values which can be stored inside variables.
let calculateAverage = function (a, b) {
return (a + b) / 2;
};
console.log(calculateAverage(10, 20)); // 15
Function Expressions are labeled as Expresssions b/c when we assign expressions to variables such as:
We can further illustrate how the function is truly stored as a variable when we call the name of the function without paramaters.
Don’t forget to add semi-colon after function expression!
Multidimensional Arrays
When we store arrays as elements of other arrays we call them multidimensional arrays
let twoDArray = [
[1, 2, 3],
[4, 5, 6],
];
console.log(twoDArray[1]); // [1,2,3]
console.log(twoDArray[1][0]); // 1
Iterating through 2D Arrays
In order to access and iterate 2D Arrays, we can use a double for loop.
To break down what is happening in a double for loop,here is a helpful illustration:
When is a 2D array practical?
Mutability : The ability to change. (mutable and immutable)
When we refer to mutability in JS we are looking to see which methods will modify existing data and which methods do not.
What is mutability? One way in which Strings and Arrays differ, is that String data is immutable.
let myArr = ["b", "e", "a", "m"];
myArr[0] = "s";
console.log(myArr); // 'seam'
let myStr = "beam";
myStr[0] = "s";
console.log(myStr); // 'beam'
let word = "piñata";
let newWord = word.toUpperCase();
console.log(word); // 'piñata'
console.log(newWord); // 'PIÑATA'
Mutable or Immutable, that is the Question
Mutable | Immutable |
---|---|
Array | Number |
Object | String |
Boolean |
The mutability misconception
What can Array.splice do?
let colors = ["red", "yellow", "blue", "green", "orange", "brown", "gray"];
let returnVal = colors.splice(2, 3);
console.log(colors); // [ 'red', 'yellow', 'brown', 'gray' ]
console.log(returnVal); // [ 'blue', 'green', 'orange' ]
Using splice to insert
let colors = ["red", "yellow", "blue"];
let returnVal = colors.splice(1, 1, "RebeccaPurple", "CornflowerBlue");
console.log(colors); // [ 'red', 'RebeccaPurple', 'CornflowerBlue', 'blue' ]
console.log(returnVal); // []
String.split
Method that can be called on a string and uses a “separator” string as an argument.
let sentence = "follow the yellow brick road";
console.log(sentence.split(" ")); // [ 'follow', 'the', 'yellow', 'brick', 'road' ]
console.log(sentence.split("the")); // [ 'follow ', ' yellow brick road' ]
console.log(sentence.split("o")); // [ 'f', 'll', 'w the yell', 'w brick r', 'ad' ]
Array.join
Method that can be called on an array and uses a “joiner” string as as argument.
let words = ["run", "around", "the", "block"];
let sentence = words.join(" ");
console.log(sentence); // 'run around the block'
console.log(words); // [ 'run', 'around', 'the', 'block' ]
console.log(words.join("_")); // 'run_around_the_block'
console.log(words.join("HI")); // 'runHIaroundHItheHIblock'
Clever Combination
We can combine these two methods to accomplish cool behavior! (will be really useful for future problem solving by changing strings to arrays and back)
let str = "I don't know what I want to eat";
let newStr = str.split("I").join("we");
console.log(newStr); // 'we don't know what we want to eat'
Array Methods (These methods all mutate our original array.)
let array = [1, 2, 3, 4, 5];
array.pop();
console.log(array); // [1, 2, 3, 4]
console.log(array.pop()); // 5
One of the handy uses of double for loops is that we can use them to find pairs of data.
let cats = ["Bob", "Fluffy", "Whiskers", "Fred", "Happy"];
function playdate(cats) {
for (var i = 0; i < cats.length; i++) {
for (var j = i + 1; j < cats.length; j++) {
console.log([cats[i], cats[j]]);
}
}
}
playdate(cats); // =>
["Fluffy", "Whiskers"][("Fluffy", "Happy")][("Whiskers", "Happy")];
Splice | Slice | |
---|---|---|
Changes Original Array | Doesn’t Change Original Array | |
Takes n arguments | Only takes two arguments | |
Arg1: Starting index | Arg1: Starting index | |
Arg2 (Optional): # of Elements to Remove | Arg 2: Ending index | |
Arg… (Optional): Element(s) to insert |
let cats = ["Fluffy", "Whiskers", "Happy"];
cats.slice(1, 3);
console.log(cats);
console.log(cats.slice(1, 3));
let cats = ["Fluffy", "Whiskers", "Happy"];
cats.splice(1, 2);
console.log(cats);