Skip to content

Instantly share code, notes, and snippets.

@ali-aka-ahmed
Last active September 2, 2022 19:00
Show Gist options
  • Save ali-aka-ahmed/0ea8b26b592ba59f03e11c03068010d1 to your computer and use it in GitHub Desktop.
Save ali-aka-ahmed/0ea8b26b592ba59f03e11c03068010d1 to your computer and use it in GitHub Desktop.
Ways to think through writing this function!
// Standard Solution
function findLongestWord(word1, word2, word3) {
if ((word1.length >= word2.length) && (word1.length >= word3.length)) {
return word1
} else if ((word2.length >= word3.length) && (word2.length >= word1.length)) {
return word2
} else if ((word3.length >= word1.length) && (word3.length >= word2.length)) {
return word3;
} else {
return 'error';
}
}
// If you're trying to write the function in a more general way, instead of checking every combination
// of words we can just cycle through them and compare them to the current longest.
function findLongestWord(word1, word2, word3) {
// assign the first word to 'longestWord'
var longestWord = word1
var otherWords = [word2, word3]
// cycle through the other words in the list, and if any of them are bigger, replace the current longest word with that word
for (var i = 0; i < otherWords.length; i++) {
var currWord = otherWords[i]
// if currWord is longer than longestWord then replace longestWord with currWord
if (currWord.length > longestWord.length) {
longestWord = currWord;
}
}
// once done cycling through list, return longestWord!
return longestWord;
}
// We can actually extrapolate this into a more general function that just takes in any list of words and returns the longest.
function findLongestWordGeneral(listOfWords) {
// validation check for list
if (listOfWords.length == 0) return 'error'
// assign the first word to 'longestWord'
var longestWord = listOfWords[0]
// cycle through all the other words in the list, and if any of them are bigger, replace the
// current longest word with that word
for (var i = 1; i < listOfWords.length; i++) {
var currWord = listOfWords[i]
// if currWord is longer than longestWord then replace longestWord with currWord
if (currWord.length > longestWord.length) {
longestWord = currWord;
}
}
// once done cycling through list, return longestWord!
return longestWord;
}
// Now that we've written a general function, we can use that as sub-routine for this function as well as others!
function findLongestWord(word1, word2, word3) {
var listOfWords = [word1, word2, word3]
return findLongestWordGeneral(listOfWords)
}
function findLongestWord(word1, word2, word3, word4, word5) {
var listOfWords = [word1, word2, word3, word4, word5]
return findLongestWordGeneral(listOfWords)
}
// If you want to do it the most efficient way however, there's probably a faster method than just cycling through each
// item in the array... here's where knowledge of algorithms comes in. But most developers in practice will use a library.
// one of the most popular: Lodash (https://lodash.com/). General rule of thumb -- if you're trying to write something efficiently,
// it's probably better to find a function in this library instead of write it yourself because under the hood it is probably
// using the most efficient algorithm.
// See https://lodash.com/docs/4.17.15#maxBy
function findLongestWordMostEfficiently(word1, word2, word3) {
return _.maxBy([word1, word2, word3], function (word) { return word.length; });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment