Skip to content

Instantly share code, notes, and snippets.

@vpivo
Created August 22, 2013 21:16
Show Gist options
  • Save vpivo/6312902 to your computer and use it in GitHub Desktop.
Save vpivo/6312902 to your computer and use it in GitHub Desktop.
ryans words
var readline = "The rain in Spain falls mainly on the plain.";
## L should be downcase
var LongestWord = function(sen) {
var words = sen.split(" ");
var longest = 0;
for (i = 0; i <= words.length; i++) {
if (longest.length < words[i].length) {
longest = words[i];
}
}
return longest;
}
## should be print(LongestWord(readline)); ##readline is variable not function
print(LongestWord(readline()));
Manual Loop Type !!!
i words[i] longest
1. 0 'the' 0
Already broke because we call longest.length on line 9, the length of 0 doesn't help us here
So lets change it:
var readline = "The rain in Spain falls mainly on the plain.";
var longestWord = function(sen) {
var words = sen.split(" ");
var longest = 0;
for (var i = 0; i < words.length; i++) { ###the var was missing here
console.log(words[i])
if (longest < words[i].length) {
longest = words[i].length; ## now we are ocmparing the longest number to the length of the current word.
word = words[i]
}
}
return word;
}
print(longestWord(readline));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment