Skip to content

Instantly share code, notes, and snippets.

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++) {
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!
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?
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]
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
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)
}
}
// 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)
}
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)
}
Array.prototype.myMap = function(callback) {
const arr = []
for (let i = 0; i < this.length; i++) {
arr.push(callback(this[i], i , this))
}
return arr
}
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) {