We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3037589 commit 0c00664Copy full SHA for 0c00664
beginner/ex9.js
@@ -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"));
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments