Notes

Function Expressions

We want to further our thinking of functions as expressions that can be stored in variables - just like other data types!


Functions as first-class objects


Two Dimensional Arrays

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


When is a 2D array practical?


Mutability

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

let myNum = 42;
myNum += 8;
console.log(myNum); //59

Array Splice Method

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); // []

Split and Join Methods

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 Push, Pop, Shift, Unshift

Array Methods (These methods all mutate our original array.)


Pairs in Arrays & Unique Pairs

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 vs Slice

Click to Read More

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);