Skip to content

Instantly share code, notes, and snippets.

@QuocCao-dev
Created September 11, 2022 09:59
Show Gist options
  • Save QuocCao-dev/386801a9a543389ac3350623694c0b16 to your computer and use it in GitHub Desktop.
Save QuocCao-dev/386801a9a543389ac3350623694c0b16 to your computer and use it in GitHub Desktop.
const players = [
{ jersey: 56, name: "Djounte Murray", position: "Point guard", PPG: 2.6 },
{ jersey: 98, name: "Dennis Rodman", position: "Small Forward", PPG: 10.8 },
{ jersey: 1, name: "Michael Jordan", position: "Small Forward", PPG: 345.6 },
{ jersey: 2, name: "Lebron James", position: "Shooting Guard", PPG: 26.7 },
{ jersey: 33, name: "Patrick Ewing", position: "Center", PPG: 20.2 },
];
const loopName = (value) =>
console.log(
`${value.jersey} - ${value.name} -- Position: ${
value.position
} - RPP: ${Math.ceil(value.PPG)}`
);
players.forEach(loopName);
const products = [
{ name: "Iphone", price: 200 },
{ name: "Motorola", price: 70 },
{ name: "Samsung", price: 150 },
{ name: "Sony", price: 98 },
{ name: "Windows pone", price: 10 },
];
const showProductInfo = (product) => {
console.log(`Name: ${product.name} Price. - Price: ${product.price}$`);
};
products.forEach(showProductInfo);
const cars = [
{ name: "Ford", price: 200 },
{ name: "Nissan", price: 400 },
{ name: "Nissan", price: 600 },
];
const conversionCurrencyUnit = (value) =>
`${value.name} is ${value.price * 200} rupies`;
const newCars = cars.map(conversionCurrencyUnit);
const channels = [
{ name: "HBO", premium: true },
{ name: "LIFE", premium: false },
{ name: "Max", premium: true },
{ name: "Cooking channel", premium: false },
{ name: "WOBI", premium: false },
];
const getPremium = (value) => value.premium;
const newArr = channels.filter(getPremium);
console.log(newArr);
// cach 1
let totalDistance = 0;
trips.forEach((trip) => {
totalDistance += trip.distance;
});
totalDistance
// cach 2
const totalDistance = trips.reduce((acc, cur) => acc + cur.distance, 0);
const result = computers.reduce(
(acc, cur) => {
if (cur.os === "Windows") acc.numWins += 1;
if (cur.os === "Mac") acc.numMacs += 1;
return acc;
},
{
numWins: 0,
numMacs: 0,
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment