-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters