-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.js
115 lines (70 loc) · 3.26 KB
/
day6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Here's the text extracted from the image:
// Activity 1: Array Creation and Access
//Task 1: Create an array of numbers from 1 to 5 and log the array to the console.
const arr = [1,2,3,4,5];
console.log(arr); //Outputs:[ 1, 2, 3, 4, 5 ]
//Task 2: Access the first and last elements of the array and log them to the console.
console.log(`${arr[0]} is the first element and ${arr[4]} is the last element`);
//Outputs:1 is the first element and 5 is the last element
//Activity 2: Array Methods (Basic)
//Task 3: Use the `push` method to add a new number to the end of the array and log the updated array.
arr.push(6,7);
console.log(arr); //Outputs:[1, 2, 3, 4,5, 6, 7]
//Task 4: Use the `pop` method to remove the last element from the array and log the updated array.
arr.pop();
console.log(arr); //Output:[ 1, 2, 3, 4, 5, 6 ]
//Task 5: Use the `shift` method to remove the first element from the array and log the updated array.
arr.shift();
console.log(arr); //Outputs:[ 2, 3, 4, 5, 6 ]
//Task 6: Use the `unshift` method to add a new number to the beginning of the array and log the updated array.
arr.unshift(8,9);
console.log(arr); //Outputs:[8, 9, 2, 3,4, 5, 6]
//Activity 3: Array Methods (Intermediate)
//Task 7: Use the `map` method to create a new array where each number is doubled and log the new array.
let newArr = arr.map(myfunction);
function myfunction(num){
return num * 10;
}
console.log(newArr); //Outputs:[80, 90, 20, 30,40, 50, 60]
//Task 8: Use the `filter` method to create a new array with only even numbers and log the new array.
const newArr1 = arr.filter(myfunction1);
function myfunction1(num1){
return num1 < 40;
}
console.log(newArr1); //Outputs:[8, 9, 2, 3,4, 5, 6]
//Task 9: Use the `reduce` method to calculate the sum of all numbers in the array and log the result.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
console.log(sum); // Output: 15
//Activity 4: Array Iteration
//Task 10: Use a `for` loop to iterate over the array and log each element to the console.
for (let i = 0; i < arr.length; i++) {
const element = arr[i];
console.log(element);
} //Outputs:8 9 2 3 4 5 6
//Task 11: Use the `forEach` method to iterate over the array and log each element to the console.
const num = [65, 44, 12, 4];
num.forEach(myFunction)
function myFunction(item, index, arr) {
arr[index] = item * 10;
}
console.log(num); //Outputs:[ 650, 440, 120, 40 ]
//Activity 5: Multi-dimensional Arrays
//Task 12: Create a two-dimensional array (matrix) and log the entire array to the console.
// Create a 2D array (matrix)
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
// Log the entire array to the console
console.log('Matrix:', matrix); //Matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
//Task 13: Access and log a specific element from the two-dimensional array.
// Create a 2D array (matrix)
const rowIndex = 1; // 2nd row (0-based index)
const colIndex = 2; // 3rd column (0-based index)
const element = matrix[rowIndex][colIndex];
console.log(`Element at row ${rowIndex + 1}, column ${colIndex + 1}:`, element);
//Output:Element at row 2, column 3: 6