-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_assignment_17.js
More file actions
123 lines (94 loc) · 2.44 KB
/
js_assignment_17.js
File metadata and controls
123 lines (94 loc) · 2.44 KB
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
116
117
118
119
120
121
122
123
1. Create a function to iterate over the following list [“John”,
“Rohn”, “Danny”, “James”]
A=function iterateList(list) {
list.forEach((item) => {
console.log(item);
});
}
// Example usage:
const list = ["John", "Rohn", "Danny", "James"];
iterateList(list);
// Output:
// John
// Rohn
// Danny
// James
2. Iterate over all the characters of the word “iNeuron”
A=const word = "iNeuron";
for(let i = 0; i < word.length; i++){
console.log(word[i]);
}
output=
i
N
e
u
r
o
n
3. Create a functional iterator, with a next function.
A=function iterator(array) {
let index = 0;
return {
next: function() {
if (index < array.length) {
return { value: array[index++], done: false };
} else {
return { done: true };
}
}
};
}
// Example usage:
const myIterator = iterator([1, 2, 3]);
console.log(myIterator.next()); // { value: 1, done: false }
console.log(myIterator.next()); // { value: 2, done: false }
console.log(myIterator.next()); // { value: 3, done: false }
console.log(myIterator.next()); // { done: true }
4. In the following two arrays find which two elements match
using iterators.
Array 1 - [“a”, “b”, “c”, “d”]
Array 2 - [“e”, “f”, “g”, “h”, “a”, “i”, “j”]
A=const arr1 = ["a", "b", "c", "d"];
const arr2 = ["e", "f", "g", "h", "a", "i", "j"];
for (let i = 0; i < arr1.length; i++) {
if (arr2.includes(arr1[i])) {
console.log(`Found a match: ${arr1[i]}`);
}
}
5. What are the different ways of iterating through an array?
demonstrate them.
A=
//Using a for loop:
const arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
//Using a forEach loop:
const arr = [1, 2, 3, 4, 5];
arr.forEach(function(item) {
console.log(item);
});
//Using a for...of loop:
const arr = [1, 2, 3, 4, 5];
for (const item of arr) {
console.log(item);
}
//Using a map function:
const arr = [1, 2, 3, 4, 5];
const newArr = arr.map(function(item) {
return item * 2;
});
console.log(newArr);
//Using a filter function:
const arr = [1, 2, 3, 4, 5];
const filteredArr = arr.filter(function(item) {
return item % 2 === 0;
});
console.log(filteredArr);
//Using a reduce function:
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce(function(total, item) {
return total + item;
}, 0);
console.log(sum);