Skip to content

About

JavaScript Homework 5 — Array Methods and Data Transformation: Practical tasks focused on mastering modern JavaScript array methods such as map(), filter(), reduce(), and toSorted(). These exercises reinforce key concepts of method chaining, immutable operations.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📘 JavaScript Homework 5

This repository contains solutions for 4 tasks focused on array methods, chaining, and data transformation in JavaScript. Each task is implemented in a separate file (task-1.js, task-2.js, task-3.js, task-4.js).


🔹 Task 1: User Names (task-1.js)

A function that returns an array of user names from an array of user objects.

const getUserNames = (users) => {
  return users.map(user => user.name);
};

✅ Test Results

getUserNames([
  { name: "Moore Hensley", email: "moorehensley@indexia.com", balance: 2811 },
  { name: "Sharlene Bush", email: "sharlenebush@tubesys.com", balance: 3821 },
  { name: "Ross Vazquez", email: "rossvazquez@xinware.com", balance: 3793 },
]);
// ["Moore Hensley", "Sharlene Bush", "Ross Vazquez"]

🔹 Task 2: Users With Specific Friend (task-2.js)

A function that filters and returns all users who have a specific friend in their friends list.

const getUsersWithFriend = (users, friendName) => {
  return users.filter(user => user.friends.includes(friendName));
};

✅ Test Results

getUsersWithFriend([
  { name: "Sharlene Bush", friends: ["Briana Decker", "Sharron Pace"] },
  { name: "Sheree Anthony", friends: ["Goldie Gentry", "Briana Decker"] },
], "Briana Decker");
// [
//   { name: "Sharlene Bush", friends: ["Briana Decker", "Sharron Pace"] },
//   { name: "Sheree Anthony", friends: ["Goldie Gentry", "Briana Decker"] }
// ]

🔹 Task 3: Sort by Friend Count (Descending) (task-3.js)

A function that sorts users in descending order by their number of friends. Uses toSorted() to avoid mutating the original array.

const sortByDescendingFriendCount = (users) => {
  return users.toSorted((a, b) => b.friends.length - a.friends.length);
};

✅ Test Results

sortByDescendingFriendCount([
  { name: "Ross Vazquez", friends: ["A", "B", "C"] },
  { name: "Sharlene Bush", friends: ["A", "B"] },
  { name: "Moore Hensley", friends: ["A"] },
]);
// [
//   { name: "Ross Vazquez", friends: ["A", "B", "C"] },
//   { name: "Sharlene Bush", friends: ["A", "B"] },
//   { name: "Moore Hensley", friends: ["A"] }
// ]

🔹 Task 4: Total Balance by Gender (task-4.js)

A function that calculates the total balance of users by gender using method chaining (filter → map → reduce).

const getTotalBalanceByGender = (users, gender) => {
  return users
    .filter(user => user.gender === gender)
    .map(user => user.balance)
    .reduce((sum, balance) => sum + balance, 0);
};

✅ Test Results

const users = [
  { name: "Moore Hensley", gender: "male", balance: 2811 },
  { name: "Sharlene Bush", gender: "female", balance: 3821 },
  { name: "Ross Vazquez", gender: "male", balance: 3793 },
  { name: "Elma Head", gender: "female", balance: 2278 },
  { name: "Carey Barr", gender: "male", balance: 3951 },
  { name: "Blackburn Dotson", gender: "male", balance: 1498 },
  { name: "Sheree Anthony", gender: "female", balance: 2764 },
];

getTotalBalanceByGender(users, "male");
// 12053

getTotalBalanceByGender(users, "female");
// 8863

📌 Summary & Reflection

With this homework, you practiced:

  • Extracting data from objects using map()
  • Filtering arrays with filter() and includes()
  • Sorting arrays immutably using toSorted()
  • Aggregating numerical data with reduce()
  • Writing clean, functional, and non-mutating JavaScript code

These exercises strengthen your understanding of array methods and help you write efficient, readable, and modern JavaScript 🚀


📌 Overview

  • Task 1: Extract user names with map()
  • Task 2: Find users with a specific friend using filter() and includes()
  • Task 3: Sort users by friend count using toSorted()
  • Task 4: Calculate total balance by gender using filter(), map(), and reduce()

About

JavaScript Homework 5 — Array Methods and Data Transformation: Practical tasks focused on mastering modern JavaScript array methods such as map(), filter(), reduce(), and toSorted(). These exercises reinforce key concepts of method chaining, immutable operations.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages