Given arrays:
const numbers = [5, 12, 8, 130, 44];
const fruits = ["apple", "banana", "cherry", "date", "elderberry"];
const mixedArray = [1, [2, 3], [4, [5, 6]], 7];
const ages = [3, 10, 18, 20, 33];
const colors = ["red", "green", "blue", "yellow"];-
Using
push()- Add
"fig"to the end of thefruitsarray. What does the updated array look like?
- Add
-
Using
pop()- Remove the last item from the
fruitsarray. What item was removed, and what does the array look like now?
- Remove the last item from the
-
Using
shift()andunshift()- Use
shift()to remove the first item from thecolorsarray. Then, add"purple"to the start usingunshift(). What is the final array?
- Use
-
Using
map()- Create a new array by doubling each value in the
numbersarray.
- Create a new array by doubling each value in the
-
Using
filter()- Create a new array from
numbersthat includes only values greater than 10.
- Create a new array from
-
Using
reduce()- Use
reduce()onnumbersto find the total sum of all elements.
- Use
-
Using
find()- Find the first number in the
agesarray that is greater than or equal to 18.
- Find the first number in the
-
Using
findIndex()- Get the index of the first even number in the
numbersarray.
- Get the index of the first even number in the
-
Using
includes()- Check if
"cherry"is present in thefruitsarray.
- Check if
-
Using
some()andevery()- Use
some()to check if there are any ages greater than 30 in theagesarray. - Use
every()to check if all numbers in thenumbersarray are positive.
- Use
-
Using
sort()- Sort the
colorsarray alphabetically. Then sort thenumbersarray in ascending order.
- Sort the
-
Using
splice()- Remove
"banana"and"cherry"from thefruitsarray usingsplice().
- Remove
-
Using
concat()- Concatenate
fruitsandcolorsinto a single array. What does the combined array look like?
- Concatenate
-
Using
slice()- Extract the first two elements from
colorswithout modifying the original array.
- Extract the first two elements from
-
Using
flat()- Flatten
mixedArrayinto a single-level array.
- Flatten
-
Using
forEach()- Log each element in the
fruitsarray to the console.
- Log each element in the
-
Using
join()- Create a single string from the
colorsarray, with each color separated by a hyphen (-).
- Create a single string from the
#1. Array Of Object
This repository provides practice questions to help users become familiar with JavaScript array methods. The exercises are based on an array of user account objects, each representing a user with properties such as id, name, email, age, balance, and isActive.
Here is the sample data you will use for the exercises:
const users = [
{ id: 1, name: "Alice", email: "alice@example.com", age: 28, balance: 5000, isActive: true },
{ id: 2, name: "Bob", email: "bob@example.com", age: 34, balance: 3000, isActive: false },
{ id: 3, name: "Charlie", email: "charlie@example.com", age: 22, balance: 7000, isActive: true },
{ id: 4, name: "Diana", email: "diana@example.com", age: 29, balance: 4000, isActive: false },
{ id: 5, name: "Eve", email: "eve@example.com", age: 35, balance: 10000, isActive: true },
];-
Find all active users:
- Use the
filtermethod to get a list of users whoseisActiveproperty istrue.
- Use the
-
Get the total balance of all users:
- Use the
reducemethod to calculate the sum of thebalanceproperty for all users.
- Use the
-
Find a user by email (
"charlie@example.com"):- Use the
findmethod to locate a user with the email"charlie@example.com".
- Use the
-
Sort users by age:
- Use the
sortmethod to order users from youngest to oldest.
- Use the
-
Increase each user's balance by 10%:
- Use the
mapmethod to create a new array of users with theirbalanceincreased by 10%.
- Use the
-
Check if any user is under 25:
- Use the
somemethod to check if there is at least one user whoseageis less than 25.
- Use the
-
Check if all users have a balance over 2000:
- Use the
everymethod to verify if all users have abalancegreater than 2000.
- Use the
-
Get a list of user names:
- Use the
mapmethod to extract just thenameproperty from each user into a new array.
- Use the
-
Count users with a balance of at least 5000:
- Use the
filtermethod to find users with a balance of 5000 or more, then get the length of the resulting array.
- Use the
-
Remove a user by ID (
3):- Use the
filtermethod to create a new array without the user whoseidis3.
- Use the
Feel free to modify and extend this README with additional exercises or details as needed. Happy coding!
This version includes only the questions, making it a good starting point for users to practice their skills with JavaScript array methods. Let me know if there's anything else you'd like to add or change!
###2. Array of Objects
Given the following array:
```javascript
const students = [
{ id: 1, name: "Alice", age: 22, scores: { math: 85, english: 78 } },
{ id: 2, name: "Bob", age: 19, scores: { math: 92, english: 65 } },
{ id: 3, name: "Charlie", age: 23, scores: { math: 88, english: 80 } },
{ id: 4, name: "David", age: 21, scores: { math: 76, english: 95 } },
{ id: 5, name: "Eve", age: 20, scores: { math: 91, english: 83 } }
];
-
Using
map()- Create a new array containing only the names of each student.
-
Using
filter()- Filter out all students who are 21 years old or older.
-
Using
reduce()- Calculate the total of all math scores.
-
Using
find()- Find the first student whose English score is above 80.
-
Using
some()andevery()- Check if any student is younger than 20.
- Verify if every student has scored above 75 in math.
-
Using
sort()- Sort the
studentsarray in ascending order by age.
- Sort the
-
Using
forEach()- Log a sentence for each student, like "Alice is 22 years old."
-
Average Math Score Challenge
- Write a function to find the average math score for all students. What is the result?
-
Top Scorer Challenge
- Write a function to find the student with the highest math score.
-
English Pass List
- Create a list of student names who scored 80 or above in English.
-
Age Grouping Challenge
- Group the students into two categories: those who are under 21 and those who are 21 and older. Return an object with two arrays.
-
Detailed Report Challenge
- Write a function that returns a report on each student in the format:
"Alice, age 22, scored 85 in math and 78 in English."
- Write a function that returns a report on each student in the format:
-
Math Improvement Challenge
- Suppose every student improves their math score by 5 points. Use
map()to create a new array reflecting these improved scores.
- Suppose every student improves their math score by 5 points. Use
-
Create a Lookup Table
- Create an object where each student’s
idis a key, and the value is the student’s name. This can be useful for quickly looking up a student’s name by theirid.
- Create an object where each student’s
-
Failed English Challenge
- Write a function that returns the names of students who scored below 70 in English.
-
Identify Unique Ages
- Use a combination of methods to find all unique ages in the
studentsarray.
- Use a combination of methods to find all unique ages in the
-
Score Range Finder
- Write a function that takes in a score range (e.g., min = 80, max = 90) and returns a list of students whose math scores fall within that range.