Skip to content

*Speaker Notes & Example Code

This is an internal document for presenting.
Data structures and algorithms??

Intro

types
variables
creating objects, arrays, and functions
if/else
We need to store all the possible rooms where the murder may have happened, but we only have strings
const billardRoom = "Billiard Room";
const kitchen = "Kitchen";
const library = "Library";
const conservatory = "Conservatory";
const hall = "Hall";
const diningRoom = "Dining Room";

let rooms = "";
I’m going to give you an easy one because, I know for a fact that it wasn’t in the library because I was in that room when the murder happened. We need to remove a room for the list. How might we do that?
First, let’s check if we have ‘Library’ in our list to begin with:
const rooms = 'Billiard Room,Kitchen,Library,Conservatory,Hall,Dining Room';

rooms.includes('Library'); // this works
rooms.indexOf('Library'); // a lot more to do here!

var item = ',Library';
var roomIndex = rooms.indexOf(item);
var firstPart = rooms.slice(0, roomIndex);
var secondPart = rooms.slice(roomIndex + item.length);

console.log(firstPart + secondPart);
Now, we need to store more than just rooms. We also have suspects and weapons to consider. How can we organize our data now if we want it to be in one variable?
// open-ended

Based on this, try out the exercise:


Arrays

Arrays are a data structure that is really similar to strings.
What are some similarities between arrays and strings that you can think of?
Now, let’s take our rooms, weapons, and suspects and make these strings an array:
const rooms = 'Billiard Room,Kitchen,Library,Conservatory,Hall,Dining Room';
const weapons = 'Revolver,Knife,Lead Pipe,Candlestick,Rope,Wrench';
const suspects = 'Mr. Green,Mrs. Peacock,Prof. Plum,Mrs. White,Miss Scarlet,Col. Mustard';
// Implement split method:

Remember how I was in the library? Well, Mr. Green was in there with me so I know that he is innocent. Let’s remove him from our array
const suspects = [
"Mr. Green",
"Mrs. Peacock",
"Prof. Plum",
"Mrs. White",
"Miss Scarlet",
"Col. Mustard"
];
What if we wanted to store all three data sets: suspects, rooms, and weapons in one variable? How would we go about doing that with an array?
const suspects = [
"Mr. Green",
"Mrs. Peacock",
"Prof. Plum",
"Mrs. White",
"Miss Scarlet",
"Col. Mustard"
];

const weapons = [
"Revolver",
"Knife",
"Lead Pipe",
"Candlestick",
"Rope",
"Wrench"
];

const rooms = [
"Billiard Room",
"Kitchen",
"Library",
"Conservatory",
"Hall",
"Dining Room"
];

const allData = [suspects, rooms, weapons];

This will work fine enough, but it is a lot of overhead to keep track of which array is suspects, which is rooms, and which is weapons. That’s why having named keys can be really handy!
What data structure might we use?

Objects

const allData = {
suspects: suspects,
rooms: rooms,
weapons: weapons
};
Dot notation and bracket notation:
How do we access the first suspect?
How do we access the last room?
// TODO


Memory Model

For this section, I have some slides with diagrams. Let’s hop over to that!


Iteration

Iteration, looping — this is what we have to do to go through a data structure. You may be looking for a particular value, you may be moving data from one place into another data structure, making a copy, etc.
In JavaScript these days there are lots of ways to loop! Let’s start with the one most of us are probably familiar with. I call this the “regular” for-loop.
for (let i = 0; i < 5; i++) {
// Runs 5 times, with values of i 0 through 4.
console.log(i);
}
A bit of a fancier loop when working with arrays is the forEach loop:
const suspectNames = [
"Miss Scarlet",
"Col. Mustard",
"Mrs. White",
"Mr. Green",
"Mrs. Peacock",
"Prof. Plum"
];

suspectNames.forEach(suspectName => {
console.log(suspectName);
});
However it doesn’t really work with objects out of the box, but there is a little bit of a trick you can do:
const weapons = {
revolver: true,
knife: true,
leadPipe: true,
candlestick: true,
rope: true,
wrench: true
};

Object.keys(weapons).forEach(weapon => {
console.log(weapon);
});
How does Object.keys work?
function getKeys(obj) {
const result = [];

for (let prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
return result;
}

console.log(getKeys({ 1: 1, 2: 3 }))
That’s a different loop. How is it different than the first for loop we discussed at the beginning?
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.