DEV Community

_Khojiakbar_
_Khojiakbar_

Posted on • Edited on

HOAM πŸ€

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']
Enter fullscreen mode Exit fullscreen mode

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']
Enter fullscreen mode Exit fullscreen mode

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".
Enter fullscreen mode Exit fullscreen mode

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?
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)