File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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" ) ) ;
You can’t perform that action at this time.
0 commit comments