Skip to content

Commit 9c3af75

Browse files
authored
Merge pull request #5 from phuocding/main
Find the Longest Word in a String
2 parents 14c1e03 + 291c8d7 commit 9c3af75

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

basic/findLongestWord.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Find the Longest Word in a String
2+
3+
Return the length of the longest word in the provided sentence.
4+
5+
Your response should be a number.
6+
7+
### 1. Answers
8+
9+
```javascript
10+
function findLongestWord(str) {
11+
12+
var arr = str.split(' ');
13+
14+
var max = 0;
15+
16+
for (var i = 0; i < arr.length; i++) {
17+
var valStr = arr[i].length;
18+
19+
if (valStr > max) {
20+
max = valStr;
21+
}
22+
}
23+
return max;
24+
}
25+
26+
findLongestWord("The quick brown fox jumped over the lazy dog");
27+
```
28+
29+
### 2. Explain
30+
31+
1. We create a arr with the str
32+
2. Setup a var for tracked the length of the bigger
33+
3. Loop inside the arr
34+
4. Setup a var for shortcut the length
35+
5. If the new one is bigger than the prev one make it the new bigger

0 commit comments

Comments
 (0)