HOAM 🏀
_Khojiakbar_
Posted on June 15, 2024
HAVE FUN WITH METHODS
map()
--------------------------map()--------------------------
// Adding xon to the end of every name
const children = ['Amir', 'Ali', 'Komila', 'Abbos', 'Aziz'];
let transformedNames = children.map(child => {
return child + 'xon'
})
console.log(transformedNames); // ['Amirxon', 'Alixon', 'Komilaxon', 'Abbosxon', 'Azizxon']
filter()
// --------------------------filter()--------------------------//
// Filter out teacher from students list.
const list = ['student1', 'student2', 'teacher', 'student3', 'student4', 'student5'];
let wantedPerson = list.filter(person => person === 'teacher')
console.log(wantedPerson) // ['teacher']
reduce()
// --------------------------reduce()--------------------------//
// Calculate the sum of the money to pay for dinner that was collected from your friends.
const money = [12000, 21000, 17500, 44000, 56000, 12500];
const collectedMoney = money.reduce((acc, perMoney) => {
return acc + perMoney;
});
console.log(`Collected money from your friends is "${collectedMoney}".`)
// Collected money from your friends is "163000".
forEach()
// --------------------------forEach()--------------------------//
// Greet with everyone
const people = ['Alisher', 'MalcolmX', 'Steve', 'Mike'];
let x = people.forEach(person => {
console.log(`Hello '${person}', How are you?`)
}); // =>
// Hello 'Alisher', How are you?
// Hello 'MalcolmX', How are you?
// Hello 'Steve', How are you?
// Hello 'Mike', How are you?
find()
// --------------------------find()--------------------------//
// Find the thieve
let crowd = ['person', 'person' , 'person', 'thieve', 'person', 'person', 'person' ];
const thieve = crowd.find(person => person === 'thieve');
console.log(thieve) // thieve
findIndex()
// --------------------------findIndex()--------------------------//
// Finding the Index of the First Odd Sock in Your Laundry Pile
let pile = ['pair', 'pair', 'pair', 'pair', 'pair', 'single', 'pair', ]
let foundItemIndex = pile.findIndex(sock => sock === 'single');
console.log(foundItemIndex) // 5
some()
// --------------------------some()--------------------------//
// Checking If Any of Your Friends Have Replied to Your Group Text About Dinner Plans
const replies = ['', '', '', 'Sure!', '']
const hasAnswer = replies.some(answer => answer !== '');
console.log(hasAnswer); // true
every()
// --------------------------every()--------------------------//
const children = [
{name: 'John', age: 30, },
{name: 'Mark', age: 30, },
{name: 'Steve', age: 30, },
{name: 'Ann', age: 30, },
{name: 'Harry', age: 30, },
]
const checkSameAge = children.every(child => child.age === 30);
console.log(checkSameAge); // true
sort()
// --------------------------sort()--------------------------//
// Sort children's name according to the alphabet
const people = [
{name: 'John', age: 30, },
{name: 'Mark', age: 30, },
{name: 'Steve', age: 30, },
{name: 'Ann', age: 30, },
{name: 'Harry', age: 30, },
]
const sortedPeople = people.sort((a, b) => a.name.localeCompare(b.name));
console.log(sortedPeople)
// {name: 'Ann', age: 30}
// {name: 'Harry', age: 30}
// {name: 'John', age: 30}
// {name: 'Mark', age: 30}
// {name: 'Steve', age: 30}
💖 💪 🙅 🚩
_Khojiakbar_
Posted on June 15, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.