Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0c00664

Browse files
fourth commit
1 parent 3037589 commit 0c00664

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎beginner/ex9.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Have the function longestWord(sen) take the sen parameter being passed and
3+
* return the largest word in the string. If there are two or more words that
4+
* are the same length, return the first word from the string with that length.
5+
* Ignore punctuation and assume sen will not be empty.
6+
*/
7+
8+
function longestWord(sen) {
9+
let validCharacters =
10+
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
11+
12+
let maxLength = 0;
13+
let longestWord = '';
14+
15+
for (let i = 0, length = 0, word = ''; i < sen.length; i++) {
16+
let c = sen[i];
17+
if (validCharacters.includes(c)) {
18+
length++;
19+
word += c;
20+
} else {
21+
length = 0;
22+
word = '';
23+
}
24+
25+
if (length > maxLength) {
26+
maxLength = length;
27+
longestWord = word;
28+
}
29+
}
30+
31+
return longestWord;
32+
}
33+
34+
console.log(longestWord("Lorem ipsum dolor sit amet, consectetur"));

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /