Skip to content

Commit 7e16a6c

Browse files
committed
find longest word 🐕
1 parent e75e24a commit 7e16a6c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: README.md

+30
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ All of these examples are from FreeCodeCamp's [course](https://www.freecodecamp.
1414
- [1. Convert Celsius to Fahrenheit](#1-convert-celsius-to-fahrenheit)
1515
- [2. Reverse a String](#2-reverse-a-string)
1616
- [3. Factorialize a Number](#3-factorialize-a-number)
17+
- [4. Find the Longest Word in a String](#4-find-the-longest-word-in-a-string)
1718

1819
## Basic Algorithm Scripting
1920

@@ -118,4 +119,33 @@ function factorialize(num) {
118119
factorialize(5);
119120
120121
122+
```
123+
124+
### 4. Find the Longest Word in a String
125+
126+
### Difficulty: Beginner
127+
128+
Return the length of the longest word in the provided sentence.
129+
130+
Your response should be a number.
131+
132+
---
133+
134+
### Solution 1
135+
136+
```
137+
function findLongestWordLength(str) {
138+
let splitWords = str.split(" ");
139+
let longestWordLength = 0;
140+
141+
for (let i = 0; i < splitWords.length; i++) {
142+
if (splitWords[i].length > longestWordLength) {
143+
longestWordLength = splitWords[i].length;
144+
}
145+
}
146+
return longestWordLength;
147+
}
148+
149+
findLongestWordLength("The quick brown fox jumped over the lazy dog");
150+
121151
```

0 commit comments

Comments
 (0)