Snippets Collections
const davidObj = {
    first: 'David',
    last: 'Mchale',
    year: 1978,
    currentYear: new Date().getFullYear(),
    age: function () {
        const yearsold = this.currentYear - this.year;
        return yearsold;
    },
};

const maryObj = {
    first: 'Mary',
    last: 'Jones',
    year: 1985, // Add this
    currentYear: new Date().getFullYear(), // Add this
};

maryObj.age = davidObj.age;

console.log(maryObj.age()); // Output should be Mary's age




// alternative cleaner way

const davidObj = {
    first: 'David',
    last: 'Mchale',
    year: 1978,
    currentYear: new Date().getFullYear(),
    age: function (year, currentYear) {
        const yearsold = currentYear - year;
        return yearsold;
    },
};

const maryObj = {
    first: 'Mary',
    last: 'Jones',
    year: 1985,
    currentYear: new Date().getFullYear(),
};

// Borrow the age method with parameters to a function outside both objects
maryObj.age = function() {
    return davidObj.age(this.year, this.currentYear);
};

console.log(maryObj.age()); // Output should be Mary's age
let myString = "Hello, World!";
let myRegex = /Hello/;
let result = myRegex.test(myString); // result === true
const animals = ['hippo', 'tiger', 'lion', 'seal', 'cheetah', 'monkey', 'salamander', 'elephant'];

const startsWithS = animals.findIndex(animal => {
  return animal[0] === 's' ? true : false;
});

console.log(startsWithS);
const randomNumbers = [375, 200, 3.14, 7, 13, 852];

// Call .filter() on randomNumbers below
const smallNumbers = randomNumbers.filter(numbers => {
  return numbers < 250;
});

console.log(smallNumbers);

const favoriteWords = ['nostalgia', 'hyperbole', 'fervent', 'esoteric', 'serene'];


// Call .filter() on favoriteWords below
const longFavoriteWords = favoriteWords.filter(words => {
  return words.length > 7;
});

console.log(longFavoriteWords);
// Primera forma

const groceries = ['brown sugar', 'salt', 'cranberries', 'walnuts'];

groceries.forEach(function(groceryItem){
  console.log(' - ' + groceryItem);
});

// Segunda forma

groceries.forEach(groceryItem => console.log(groceryItem));

// Tercera forma

function printGrocery(element){
  console.log(element);
}
 
groceries.forEach(printGrocery);
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];

const pastaIndex = groceryList.indexOf('pasta');

console.log(pastaIndex);
const groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains'];

groceryList.shift();
console.log(groceryList);

groceryList.unshift('popcorn');
console.log(groceryList);

console.log(groceryList.slice(1,4));

console.log(groceryList);
star

Wed Jun 19 2024 01:06:04 GMT+0000 (Coordinated Universal Time)

#this #object #method #borrowing
star

Sat Dec 18 2021 16:52:43 GMT+0000 (Coordinated Universal Time)

#javascript #test #method
star

Tue Oct 05 2021 13:00:40 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/find-index

#javascript #method
star

Tue Oct 05 2021 12:49:17 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/filter

#javascript #method
star

Tue Oct 05 2021 11:54:02 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/javascript-iterators/exercises/for-each

#javascript #method
star

Fri Oct 01 2021 11:46:59 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/arrays/exercises/more-array-methods

#javascript #method
star

Fri Oct 01 2021 11:44:02 GMT+0000 (Coordinated Universal Time) https://www.codecademy.com/courses/introduction-to-javascript/lessons/arrays/exercises/more-array-methods

#javascript #method

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension