This file contains hidden or 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
| const nums = [2, 6, 3, 10] | |
| const nums2 = [2, 4, 1, 2] | |
| const duplicates2 = arr => { | |
| for (let i = 0; i < arr.length; i++) { | |
| // Cut down on our work by not considering pairs | |
| // we have already looked at | |
| for (let j = i + 1; j < arr.length; j++) { |
This file contains hidden or 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
| const nums = [2, 6, 3, 10] | |
| const nums2 = [2, 4, 1, 2] | |
| const duplicates = arr => { | |
| for (let i = 0; i < arr.length; i++) { | |
| for (let j = 0; j < arr.length; j++) { | |
| // If the indexes do not match but the | |
| // values do, we have a duplicate! |
This file contains hidden or 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
| const vetApprovedDogs = [ | |
| {name: 'Snookums', needsShots: false, breed: 'pug', age: 2}, | |
| {name: 'Thor', needsShots: false, breed: 'hound', age: 1}, | |
| {name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9}, | |
| ] | |
| // The big report for the city, have to be sure we get this right! | |
| const reportForCity = vetApprovedDogs.myReduce((report, dog) => { | |
| // Is this breed not in the report? |
This file contains hidden or 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
| const vetApprovedDogs = [ | |
| {name: 'Snookums', needsShots: false, breed: 'pug', age: 2}, | |
| {name: 'Thor', needsShots: false, breed: 'hound', age: 1}, | |
| {name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9}, | |
| ] | |
| // I need a little more practice with map! | |
| const allDogsAges = vetApprovedDogs.map(dog => dog.age) | |
| console.log(allDogsAges) // [2, 1, 9] |
This file contains hidden or 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
| const dogs = [ | |
| {name: 'Snookums', needsShots: true, breed: 'pug', age: 2}, | |
| {name: 'Thor', needsShots: true, breed: 'hound', age: 1}, | |
| {name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9}, | |
| ] | |
| // We are going to create a new array of our vet approved dogs! | |
| const vetApprovedDogs = dogs.myMap((dog) => { | |
| // The vet makes his approval |
This file contains hidden or 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
| Array.prototype.myFilter = function(callback) { | |
| const arr = [] | |
| for (let i = 0; i < this.length; i++) { | |
| const item = this[i] | |
| if(callback(item, i, this)) { | |
| arr.push(item) | |
| } | |
| } |
This file contains hidden or 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
| // filter | |
| Array.prototype.myFilter = function(func) { | |
| const arr = [] | |
| for (let i = 0; i < this.length; i++) { | |
| const item = this[i] | |
| if(func(item, i, this)) { | |
| arr.push(item) | |
| } |
This file contains hidden or 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
| Array.prototype.myReduce = function(func, initialValue) { | |
| const arr = arguments.length > 1 | |
| ? [initialValue, ...this] | |
| : this | |
| let accumulator = arr[0] | |
| for (let i = 1; i < arr.length; i++) { | |
| accumulator = func(accumulator, arr[i], i, this) | |
| } |
This file contains hidden or 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
| Array.prototype.myMap = function(callback) { | |
| const arr = [] | |
| for (let i = 0; i < this.length; i++) { | |
| arr.push(callback(this[i], i , this)) | |
| } | |
| return arr | |
| } |
This file contains hidden or 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
| const dogs = [ | |
| {name: 'Snookums', needsShots: true, breed: 'pug', age: 2}, | |
| {name: 'Thor', needsShots: true, breed: 'hound', age: 1}, | |
| {name: 'Wunderbar', needsShots: false, breed: 'pug', age: 9}, | |
| ] | |
| // Lets see how a filter method might work... | |
| const dogsToSeeVet = dogs.myFilter(function(dog) { | |