You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Problems broken down into steps/coderbyte.js
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -26,3 +26,26 @@ function FirstFactorial(num) {
26
26
}
27
27
returnnum*FirstFactorial(num-1);
28
28
}
29
+
30
+
// Steps located to the right of code.
31
+
32
+
functionLongestWord(sen){
33
+
varwords=sen.split(' ');// Split the user-inputted sentence into words
34
+
varcounter=0;// Create a counter to keep track of the # of letters in a word
35
+
varpoleposcount=0;// Create a pole position counter to keep track of the # of letters the longest word has
36
+
varpoleposword='';// Create an empty string to store the longest word
37
+
for(vari=0;i<words.length;i++){// Create a FOR LOOP to run thru each word in the sentence. FOR EACH word...
38
+
varsideword=words[i];// Store the current word in a side variable for later use
39
+
for(varj=0;j<sideword.length;j++){// Create another FOR LOOP to run thru each character in the current side word. FOR EACH character...
40
+
if(sideword[j]>='a'&&sideword[j]<='z'){// So long as the character is a letter...
41
+
counter+=1;// -> Add 1 to the counter (this counts the # of letters in the current side word)
42
+
}
43
+
if(counter>poleposcount){// After running thru each letter, if the counter (# of letters in the current word) is greater than the pole position counter (# of letters in the longest word)...
44
+
poleposcount=counter;// -> Assign the counter as the new pole position counter (the current value for the counter becomes the value of the pole position counter)
45
+
poleposword=sideword;// -> Assign the current side word as the pole position word aka the current word becomes the current longest word (so far at least)
46
+
}
47
+
}
48
+
counter=0;// Reset the counter at the end of the 1st FOR-LOOP
0 commit comments