Skip to content

Commit b00f961

Browse files
committed
Challenge 23 & 24 Added
1 parent cf0547b commit b00f961

File tree

7 files changed

+148
-0
lines changed

7 files changed

+148
-0
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,22 @@ Write a function called `isLeapYear` that takes `year` in its parameter and retu
202202

203203
[Solution Explanation](./solutions/ch_22_Check_Leap_Year/readme.md)
204204

205+
## Challenge 23: Sum of Array Elements
206+
207+
Write a JavaScript function to sum of all elements in an array.
208+
209+
Write a function called `sumArray` that takes `array` in its parameter and returns sum of all array elements.
210+
211+
[Solution Explanation](./solutions/ch_23_Sum_Of_Array_Elements/readme.md)
212+
213+
## Challenge 24: Find Longest Word in Sentence
214+
215+
Write a function to find the longest word in a sentence.
216+
217+
Write a function called `findLongestWord` that takes string as argument `sentence` of type string and return longest word in sentence.
218+
219+
[Solution Explanation](./solutions/ch_24_Find_Longest_Word_In_Sentence/readme.md)
220+
205221
<!-- Add new challenges before this comment -->
206222

207223
## Contributors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Function to sum array elements
2+
function sumArray(array) {
3+
let sum = 0;
4+
5+
// Run loop
6+
for (let i = 0; i < array.length; i++) {
7+
// Add array element to sum
8+
sum += array[i]; // sum = sum + array[i];
9+
}
10+
return sum;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Function to sum array elements
2+
function sumArray(array) {
3+
let sum = 0;
4+
5+
// Use array forEach method
6+
array.forEach(element => {
7+
// Add element to sum
8+
sum += element; // sum = sum + element;
9+
});
10+
11+
return sum;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Function to sum array elements
2+
function sumArray(array) {
3+
// Use array reduce method
4+
return array.reduce((sum, num) => sum + num, 0);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Challenge 23: Sum of Array Elements
2+
3+
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+
function sumArray(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+
function sumArray(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+
function sumArray(array) {
53+
// Use array reduce method
54+
return array.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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Function to find longest word in sentence
2+
function findLongestWord(sentence) {
3+
// Split sentence words into array of words
4+
const words = sentence.split(' ');
5+
6+
let longestWord = '';
7+
8+
// Run for of loop
9+
for (let word of words) {
10+
if (word.length > longestWord.length) {
11+
longestWord = word;
12+
}
13+
}
14+
return longestWord;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Challenge 24: Find Longest Word in Sentence
2+
3+
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+
function findLongestWord(sentence) {
12+
// Split sentence words into array of words
13+
const words = 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

Comments
 (0)