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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay