Assignments Of Day 19: More Advanced Array Methods
Assignment
1: Using map() to Double Array Values
Problem:
You are
given an array of numbers: [3, 5, 7, 10, 15]. Write a JavaScript program to
create a new array that contains each number in the original array doubled.
Solution:
To solve
this, we will use the map() method to create a new array where each element is
the double of the corresponding element in the original array.
javascript
Copy code
let numbers = [3, 5, 7, 10, 15];
let doubledNumbers = numbers.map(function(num) {
return num
* 2;
});
console.log(doubledNumbers); // Output: [6, 10, 14,
20, 30]
Explanation:
- The
map() method creates a new array by multiplying each number in the
original array by 2.
- The
result is [6, 10, 14, 20, 30].
Assignment
2: Filtering Even Numbers Using filter()
Problem:
You are
given an array of numbers: [3, 5, 6, 7, 8, 10]. Write a JavaScript program that
returns a new array containing only the even numbers from the original array.
Solution:
We can
use the filter() method to filter out only the even numbers.
javascript
Copy code
let numbers = [3, 5, 6, 7, 8, 10];
let evenNumbers = numbers.filter(function(num) {
return num
% 2 === 0;
});
console.log(evenNumbers); // Output: [6, 8, 10]
Explanation:
- The
filter() method loops through each element in the array and returns only
those that satisfy the condition (num % 2 === 0), which checks if the
number is even.
- The
result is [6, 8, 10].
Assignment
3: Using reduce() to Sum All Numbers
Problem:
You are
given an array of numbers: [5, 10, 15, 20]. Write a JavaScript program that
sums all the numbers in the array using reduce().
Solution:
We will
use the reduce() method to accumulate the sum of all numbers in the array.
javascript
Copy code
let numbers = [5, 10, 15, 20];
let sum = numbers.reduce(function(accumulator, num)
{
return
accumulator + num;
}, 0);
console.log(sum); // Output: 50
Explanation:
- The
reduce() method takes two arguments: an accumulator (which stores the
result) and the current element (num).
- The
accumulator starts at 0 (initial value), and in each iteration, it adds
the current number to the accumulator.
- The
result is 50.
Assignment
4: Using forEach() to Print Each Element
Problem:
You are given
an array of names: ["John", "Jane", "Tom",
"Anna"]. Write a JavaScript program that prints each name to the
console using the forEach() method.
Solution:
We will
use the forEach() method to print each element in the array.
javascript
Copy code
let names = ["John", "Jane",
"Tom", "Anna"];
names.forEach(function(name) {
console.log(name);
});
Explanation:
- The
forEach() method loops over each element of the names array and executes
the provided function for each element.
- It
prints each name in the array to the console.
Assignment
5: Finding First Number Greater Than 10 Using find()
Problem:
You are
given an array of numbers: [4, 8, 12, 20]. Write a JavaScript program that
finds the first number in the array that is greater than 10 using the find()
method.
Solution:
We will
use the find() method to locate the first number greater than 10.
javascript
Copy code
let numbers = [4, 8, 12, 20];
let firstGreaterThanTen =
numbers.find(function(num) {
return num
> 10;
});
console.log(firstGreaterThanTen); // Output: 12
Explanation:
- The
find() method returns the first element that satisfies the condition (num
> 10).
- It
stops iterating once it finds the first matching element. Here, the first
number greater than 10 is 12.
Assignment
6: Checking if Some Numbers are Odd Using some()
Problem:
You are
given an array of numbers: [2, 4, 6, 8, 10]. Write a JavaScript program that
checks if there is at least one odd number in the array using the some()
method.
Solution:
We will use
the some() method to check if there is at least one odd number in the array.
javascript
Copy code
let numbers = [2, 4, 6, 8, 10];
let hasOdd = numbers.some(function(num) {
return num
% 2 !== 0;
});
console.log(hasOdd); // Output: false
Explanation:
- The
some() method checks if at least one element satisfies the condition (num
% 2 !== 0).
- In
this case, the array contains only even numbers, so the result is false.
Assignment
7: Checking if All Numbers are Even Using every()
Problem:
You are given
an array of numbers: [2, 4, 6, 8, 10]. Write a JavaScript program that checks
if all numbers in the array are even using the every() method.
Solution:
We will
use the every() method to check if all elements are even.
javascript
Copy code
let numbers = [2, 4, 6, 8, 10];
let allEven = numbers.every(function(num) {
return num
% 2 === 0;
});
console.log(allEven); // Output: true
Explanation:
- The
every() method checks if all elements in the array satisfy the condition
(num % 2 === 0).
- Since
all numbers in the array are even, the result is true.
Assignment
8: Combining Methods - Mapping, Filtering, and Summing
Problem:
You are
given an array of numbers: [3, 6, 9, 12, 15]. Write a program to:
1. Double
each number using map().
2. Filter
out the even numbers using filter().
3. Calculate
the sum of the filtered numbers using reduce().
Solution:
We will
combine map(), filter(), and reduce() to perform the required operations in
sequence.
javascript
Copy code
let numbers = [3, 6, 9, 12, 15];
// Step 1: Double the numbers using map()
let doubled = numbers.map(function(num) {
return num
* 2;
});
// Step 2: Filter out even numbers using filter()
let evenDoubled = doubled.filter(function(num) {
return num
% 2 === 0;
});
// Step 3: Calculate the sum of the filtered
numbers using reduce()
let sum = evenDoubled.reduce(function(acc, num) {
return acc
+ num;
}, 0);
console.log(sum); // Output: 60
Explanation:
1. The map()
method doubles each number: [6, 12, 18, 24, 30].
2. The
filter() method filters out even numbers: [6, 12, 18, 24, 30].
3. The
reduce() method calculates the sum of the filtered numbers: 6 + 12 + 18 + 24 +
30 = 60.
These
assignments will help you become proficient in using advanced array methods
like map(), filter(), reduce(), forEach(), find(), some(), and every() in
JavaScript. Practice these problems to reinforce your understanding.
4o mini
You said:
give more
ChatGPT
said:
ChatGPT
Here are
some additional assignments to help you practice and understand advanced array methods
in JavaScript:
Assignment
9: Transforming Array of Strings Using map()
Problem:
You are
given an array of strings: ["apple", "banana",
"cherry", "date"]. Write a program to convert all the
strings in the array to uppercase using the map() method.
Solution:
We will
use the map() method to create a new array where all strings are in uppercase.
javascript
Copy code
let fruits = ["apple",
"banana", "cherry", "date"];
let uppercasedFruits = fruits.map(function(fruit) {
return
fruit.toUpperCase();
});
console.log(uppercasedFruits); // Output:
["APPLE", "BANANA", "CHERRY", "DATE"]
Explanation:
- The
map() method transforms each string in the array to uppercase using
toUpperCase().
- The
result is ["APPLE", "BANANA", "CHERRY",
"DATE"].
Assignment
10: Filtering Out Numbers Greater Than 10 Using filter()
Problem:
You are
given an array of numbers: [2, 8, 11, 15, 7, 3]. Write a program to filter out
numbers that are greater than 10 using the filter() method.
Solution:
We will
use the filter() method to create a new array containing only the numbers less
than or equal to 10.
javascript
Copy code
let numbers = [2, 8, 11, 15, 7, 3];
let numbersLessThanOrEqualToTen =
numbers.filter(function(num) {
return num
<= 10;
});
console.log(numbersLessThanOrEqualToTen); //
Output: [2, 8, 7, 3]
Explanation:
- The
filter() method returns an array of numbers that are <= 10.
- The
result is [2, 8, 7, 3].
Assignment
11: Using reduce() to Multiply All Elements
Problem:
You are
given an array of numbers: [1, 2, 3, 4]. Write a program that multiplies all
the numbers together using the reduce() method.
Solution:
We will
use the reduce() method to calculate the product of all the elements in the
array.
javascript
Copy code
let numbers = [1, 2, 3, 4];
let product = numbers.reduce(function(accumulator,
num) {
return
accumulator * num;
}, 1);
console.log(product); // Output: 24
Explanation:
- The
reduce() method multiplies all the elements together starting with an
initial value of 1.
- The result
is 1 * 2 * 3 * 4 = 24.
Assignment
12: Using forEach() to Print Each Element with Index
Problem:
You are
given an array of numbers: [10, 20, 30, 40]. Write a program that prints each
number along with its index using the forEach() method.
Solution:
We will
use the forEach() method and access both the value and index of each element.
javascript
Copy code
let numbers = [10, 20, 30, 40];
numbers.forEach(function(num, index) {
console.log(`Index: ${index}, Value: ${num}`);
});
Explanation:
- The
forEach() method iterates over each element of the array and prints its
index and value.
- The
result will be:
yaml
Copy code
Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Assignment
13: Using find() to Find a Negative Number
Problem:
You are
given an array of numbers: [5, 8, -4, 12, -7]. Write a program to find the
first negative number in the array using the find() method.
Solution:
We will
use the find() method to locate the first negative number in the array.
javascript
Copy code
let numbers = [5, 8, -4, 12, -7];
let firstNegative = numbers.find(function(num) {
return num
< 0;
});
console.log(firstNegative); // Output: -4
Explanation:
- The find()
method stops once it finds the first element that is negative, which is
-4.
- The
result is -4.
Assignment
14: Checking If Any Numbers Are Divisible by 3 Using some()
Problem:
You are
given an array of numbers: [5, 7, 9, 12]. Write a program to check if any of
the numbers are divisible by 3 using the some() method.
Solution:
We will
use the some() method to check if any number in the array is divisible by 3.
javascript
Copy code
let numbers = [5, 7, 9, 12];
let hasDivisibleByThree = numbers.some(function(num)
{
return num
% 3 === 0;
});
console.log(hasDivisibleByThree); // Output: true
Explanation:
- The
some() method checks if any number in the array satisfies the condition
(num % 3 === 0).
- Since
9 and 12 are divisible by 3, the result is true.
Assignment
15: Checking If All Numbers Are Positive Using every()
Problem:
You are
given an array of numbers: [-2, 4, 6, -8]. Write a program to check if all the
numbers are positive using the every() method.
Solution:
We will
use the every() method to check if all the numbers are greater than 0.
javascript
Copy code
let numbers = [-2, 4, 6, -8];
let allPositive = numbers.every(function(num) {
return num
> 0;
});
console.log(allPositive); // Output: false
Explanation:
- The
every() method checks if all the elements in the array satisfy the
condition (num > 0).
- Since
the array contains negative numbers, the result is false.
Assignment
16: Mapping Objects to Extract Specific Properties
Problem:
You are
given an array of objects, each representing a person with name and age. Write
a program to create a new array containing only the names of the people.
javascript
Copy code
let people = [
{ name:
"John", age: 28 },
{ name:
"Jane", age: 32 },
{ name:
"Tom", age: 25 },
{ name:
"Anna", age: 40 }
];
let names = people.map(function(person) {
return
person.name;
});
console.log(names); // Output: ["John",
"Jane", "Tom", "Anna"]
Explanation:
- The
map() method is used to extract the name property from each object in the
array.
- The
result is ["John", "Jane", "Tom",
"Anna"].
Assignment
17: Using map(), filter(), and reduce() Together
Problem:
You are
given an array of numbers: [1, 2, 3, 4, 5, 6]. Write a program that:
1. Doubles
the numbers using map().
2. Filters
out numbers greater than 8 using filter().
3. Calculates
the sum of the remaining numbers using reduce().
Solution:
We will
use map(), filter(), and reduce() in sequence.
javascript
Copy code
let numbers = [1, 2, 3, 4, 5, 6];
let sum = numbers
.map(function(num) {
return
num * 2;
})
.filter(function(num) {
return
num <= 8;
})
.reduce(function(acc, num) {
return
acc + num;
}, 0);
console.log(sum); // Output: 12
Explanation:
1. The map()
method doubles the numbers: [2, 4, 6, 8, 10, 12].
2. The
filter() method filters out numbers greater than 8: [2, 4, 6, 8].
3. The
reduce() method calculates the sum of the filtered numbers: 2 + 4 + 6 + 8 = 12.
