Skip to content

Commit ef058e3

Browse files
author
Kyle Maune
committed
added longest word and breakdown for it
1 parent 6591f80 commit ef058e3

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Problems broken down into steps/coderbyte.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,26 @@ function FirstFactorial(num) {
2626
}
2727
return num * FirstFactorial(num - 1);
2828
}
29+
30+
// Steps located to the right of code.
31+
32+
function LongestWord(sen) {
33+
var words = sen.split(' '); // Split the user-inputted sentence into words
34+
var counter = 0; // Create a counter to keep track of the # of letters in a word
35+
var poleposcount = 0; // Create a pole position counter to keep track of the # of letters the longest word has
36+
var poleposword = ''; // Create an empty string to store the longest word
37+
for (var i = 0; i < words.length; i++){ // Create a FOR LOOP to run thru each word in the sentence. FOR EACH word...
38+
var sideword = words[i]; // Store the current word in a side variable for later use
39+
for (var j = 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
49+
}
50+
return poleposword; // Return the longest word
51+
}

0 commit comments

Comments
 (0)