Skip to content

Instantly share code, notes, and snippets.

@Luke-Rogerson
Created May 1, 2018 05:44
Show Gist options
  • Save Luke-Rogerson/5c779ad0b9d22f9b5229ffa822139e7f to your computer and use it in GitHub Desktop.
Save Luke-Rogerson/5c779ad0b9d22f9b5229ffa822139e7f to your computer and use it in GitHub Desktop.
LongestWord algorithm challenge created by Luke_Rogerson - https://repl.it/@Luke_Rogerson/LongestWord
/*
have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.
*/
function LongestWord(sen) {
let longestWord = "";
let sentence = sen.match(/[a-z0-9]+/gi);
// let sentenceArray = sentence.split(" ");
console.log(sentence);
for (let i = 0; i < sentence.length; i++) {
if (sentence[i].length > longestWord.length ) {
longestWord = sentence[i];
}
else if (sentence[i].length === longestWord.length) {
continue;
}
}
return longestWord;
}
LongestWord("I love£ dogs");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment