Skip to content

Commit

Permalink
A few more updates
Browse files Browse the repository at this point in the history
  • Loading branch information
chellman committed Mar 22, 2023
1 parent feb80e8 commit f9720a9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 95 deletions.
9 changes: 7 additions & 2 deletions Ch05/05_02/transcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ var pageNames = [
"Contact Us",
"JavaScript Playground",
"News",
"Blog"
"Blog",
];

for (let i = 0; i < pageNames.length; i += 1) {
console.log(i, pageNames[i]);
}

for (var p in pageNames) {
console.log(p, pageNames[p]);
}
Expand All @@ -21,7 +26,7 @@ var pages = {
second: "About Us",
third: "Contact Us",
fourth: "JavaScript Playground",
fifth: "Blog"
fifth: "Blog",
};
for (var p in pages) {
if (pages.hasOwnProperty(p)) {
Expand Down
87 changes: 48 additions & 39 deletions Ch05/05_03/transcript.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
// for loop
for (let i = 0; i < 10; i++) {
console.log(i);
// Creating a new Map instance
var theMap = new Map();

// This object is a bird
var bird = {
genus: "corvus",
species: "corvax",
commonName: "raven",
};

// Here is a map using the same structure
var birdMap = new Map();
birdMap.set("genus", "corvus");
birdMap.set("species", "corvax");
birdMap.set("commonName", "raven");

birdMap.get("genus"); // 'corvus'

birdMap.has("genus"); // true
birdMap.has("corvus"); // false (keys only)

// for...of loops work on Maps, with key and value returned
for (let value of birdMap) {
console.log(`${value[0]} is ${value[1]}`);
}

// same thing as a while loop
let i = 0;
while (i < 10) {
console.log(i + "... This will go until we hit 10");
i += 1;
// With destructuring, those parts can be unpacked into separate variables
for (let [key, value] of birdMap) {
console.log(`${key} is ${value}`);
}

var myList = [true, true, true, false, true, true];
// Object.entries turns an object into the nested arrays that can make a Map
var birdMap2 = new Map(Object.entries(bird));

var myItem = null;
// This array has two copies of its first item
let myList = [1, 1, 2, 3, 5, 8, 13, "fibonacci"];

while (myItem !== false) {
console.log(
"myList has " +
myList.length +
" items now. This loop will keep going until we pop a false."
);
myItem = myList.pop();
}
// The same thing using the Set API
let mySet = new Set();
mySet.add(1);
mySet.add(1); // this won't change mySet, since 1 is already in there
mySet.add(2);
mySet.add(3);
// ... this gets tedious

var counter = 1;
while (true) {
console.log(counter);
counter++;
break; // comment out this break statement to make this loop go forever and potentially lock up your web browser
}
// An array can be turned into a set
// If you want to quickly remove all duplicates from an array, this is a good tool!
let mySet2 = new Set(myList);

var myList = [true, true, true, false, true, true];

var myItem = false;
do {
console.log(
"myList has " +
myList.length +
" items now. This loop will go until we pop a false."
);
myItem = myList.pop();
} while (myItem !== false);

// More info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while
mySet2.has(3); // true
mySet2.has(12); // false

// For...of loop iteration works
for (let item of mySet2) {
console.log(`${item} is contained in mySet`);
}
87 changes: 39 additions & 48 deletions Ch05/05_04/transcript.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,47 @@
// Creating a new Map instance
var theMap = new Map();

// This object is a bird
var bird = {
genus : 'corvus',
species : 'corvax',
commonName: 'raven'
};

// Here is a map using the same structure
var birdMap = new Map();
birdMap.set('genus', 'corvus');
birdMap.set('species', 'corvax');
birdMap.set('commonName', 'raven');

birdMap.get('genus'); // 'corvus'

birdMap.has('genus'); // true
birdMap.has('corvus'); // false (keys only)

// for...of loops work on Maps, with key and value returned
for (let value of birdMap) {
console.log(`${value[0]} is ${value[1]}`);
// for loop
for (let i = 0; i < 10; i++) {
console.log(i);
}

// With destructuring, those parts can be unpacked into separate variables
for (let [key, value] of birdMap) {
console.log(`${key} is ${value}`);
// same thing as a while loop
let i = 0;
while (i < 10) {
console.log(i + "... This will go until we hit 10");
i += 1;
}

// Object.entries turns an object into the nested arrays that can make a Map
var birdMap2 = new Map(Object.entries(bird));
var myList = [true, true, true, false, true, true];

// This array has two copies of its first item
let myList = [1, 1, 2, 3, 5, 8, 13, 'fibonacci'];
var myItem = null;

// The same thing using the Set API
let mySet = new Set();
mySet.add(1);
mySet.add(1); // this won't change mySet, since 1 is already in there
mySet.add(2);
mySet.add(3);
// ... this gets tedious

// An array can be turned into a set
// If you want to quickly remove all duplicates from an array, this is a good tool!
let mySet2 = new Set(myList);
while (myItem !== false) {
console.log(
"myList has " +
myList.length +
" items now. This loop will keep going until we pop a false."
);
myItem = myList.pop();
}

mySet2.has(3); // true
mySet2.has(12); // false
var counter = 1;
while (true) {
console.log(counter);
counter++;
break; // comment out this break statement to make this loop go forever and potentially lock up your web browser
}

// For...of loop iteration works
for (let item of mySet2) {
console.log(`${item} is contained in mySet`);
}
var myList = [true, true, true, false, true, true];

var myItem = false;
do {
console.log(
"myList has " +
myList.length +
" items now. This loop will go until we pop a false."
);
myItem = myList.pop();
} while (myItem !== false);

// More info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while
4 changes: 2 additions & 2 deletions Ch06/06_03/transcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ function speakSomething(what = "Default speech", howMany = 10) {
}
}

speakSomething("Hey hey", 5);
speakSomething("Hey hey");
speakSomething("Good morning", 5);
speakSomething("Good morning");
speakSomething();

function addingMachine() {
Expand Down
8 changes: 4 additions & 4 deletions Ch06/06_04/transcript.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var calvin = {
name: "Calvin",
bestFriend: "Hobbes",
form: "human boy"
form: "human boy",
};

// you can also pass an object to a function
Expand Down Expand Up @@ -47,7 +47,7 @@ function transmogrifyCopy(calvin) {

// generate a random number between 1 and 5
var randomNumber = Math.floor(Math.random() * 5) + 1;

var newForm = calvin.form; // by default, do not change

switch (randomNumber) {
Expand Down Expand Up @@ -77,11 +77,11 @@ function transmogrifyCopy(calvin) {
return {
name: calvin.name,
bestFriend: calvin.bestFriend,
form: newForm
form: newForm,
};
}

// More info:
// https://en.wikipedia.org/wiki/Calvin_and_Hobbes
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function

0 comments on commit f9720a9

Please sign in to comment.