You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write a JavaScript function to sum of all elements in an array.
4
+
5
+
Write a function called `sumArray` that takes `array` in its parameter and returns sum of all array elements.
6
+
7
+
## Answer 1
8
+
9
+
```javascript
10
+
// Function to sum array elements
11
+
functionsumArray(array) {
12
+
let sum =0;
13
+
14
+
// Run loop
15
+
for (let i =0; i <array.length; i++) {
16
+
// Add array element to sum
17
+
sum += array[i]; // sum = sum + array[i];
18
+
}
19
+
return sum;
20
+
}
21
+
```
22
+
23
+
## Answer 1 Explanation
24
+
25
+
The `sumArray` function takes `array` as input. Declare `sum` variable to store sum of all array elements. Run loop for each array element and add array element to `sum` and finally return `sum`.
26
+
27
+
## Answer 2 (Array forEach Method)
28
+
29
+
```javascript
30
+
// Function to sum array elements
31
+
functionsumArray(array) {
32
+
let sum =0;
33
+
34
+
// Use array forEach method
35
+
array.forEach((element) => {
36
+
// Add element to sum
37
+
sum += element; // sum = sum + element;
38
+
});
39
+
40
+
return sum;
41
+
}
42
+
```
43
+
44
+
## Answer 2 Explanation
45
+
46
+
The `sumArray` function takes `array` as input. Declare `sum` variable to store sum of all array elements. Use array `forEach()` method which run it for each array element and add array element to `sum` and return `sum`.
47
+
48
+
## Answer 3 (Array Reduce Method)
49
+
50
+
```javascript
51
+
// Function to sum array elements
52
+
functionsumArray(array) {
53
+
// Use array reduce method
54
+
returnarray.reduce((sum, num) => sum + num, 0);
55
+
}
56
+
```
57
+
58
+
## Answer 3 Explanation
59
+
60
+
The `sumArray` function takes `array` as input and it uses the `reduce()` method to iterate over the array and return the sum of all elements.
Write a function to find the longest word in a sentence.
4
+
5
+
Write a function called `findLongestWord` that takes string as argument `sentence` of type string and return longest word in sentence.
6
+
7
+
## Answer
8
+
9
+
```javascript
10
+
// Function to find longest word in sentence
11
+
functionfindLongestWord(sentence) {
12
+
// Split sentence words into array of words
13
+
constwords=sentence.split("");
14
+
15
+
let longestWord ="";
16
+
17
+
// Run for of loop
18
+
for (let word of words) {
19
+
if (word.length>longestWord.length) {
20
+
longestWord = word;
21
+
}
22
+
}
23
+
return longestWord;
24
+
}
25
+
```
26
+
27
+
## Answer Explanation
28
+
29
+
The function `findLongestWord` takes string as argument and convert all words of `sentence` in array of word using `split()` method and array of word called `words`. After that using for of loop for each word checking condition if any word's length is more than current `longestWord's` length assign that word in `longestWord`.
0 commit comments