From 62d8925c7281e1cfe289ecf02544c5b8379ec201 Mon Sep 17 00:00:00 2001 From: Muhammad Shoaib Date: Tue, 6 Oct 2020 19:39:12 +0500 Subject: [PATCH 1/2] added longest words in a string code --- .../longest_words_in_string.js | 22 ++++ README.md | 114 +++++++++++++++++- 2 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 Longest Words In String/longest_words_in_string.js diff --git a/Longest Words In String/longest_words_in_string.js b/Longest Words In String/longest_words_in_string.js new file mode 100644 index 0000000..0423819 --- /dev/null +++ b/Longest Words In String/longest_words_in_string.js @@ -0,0 +1,22 @@ +function longestWords(sentence) { + let arrayOfMaxWords = [] + if (sentence) { + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + if (word.length > maxLength) { + maxLength = word.length + arrayOfMaxWords = [] + arrayOfMaxWords.push(word) + } + else if (word.length === maxLength && word !== arrayOfMaxWords[0]) { + arrayOfMaxWords.push(word) + } + }); + } + return arrayOfMaxWords; +} +console.log(longestWords("You are just an old antidisestablishmentarian")) +console.log(longestWords("I gave a present to my parents")) +console.log(longestWords("Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo")) \ No newline at end of file diff --git a/README.md b/README.md index 5e7e529..708be17 100644 --- a/README.md +++ b/README.md @@ -700,15 +700,121 @@ It's that simple! Hope this helps.

-8. Name + Longest Words In A String -__The challenge:__

+__The challenge:__

In this challenge, we will find out a solution of how to find longest word/words in a given string

-__Algorithmic Thinking:__

+__Algorithmic Thinking:__

Split the given string with space and save it in an array. Now the array has each word of the string. Iterating over each word we will calculate its length and determine whether this word length is greater with the previous one then push it in a new array and will return it at the end.

-__code Implementation:__

+__code Implementation:__

First we will make an empty array of arrayOfMaxWords +```js + let arrayOfMaxWords = [] +``` +then we will check whether string is empty or not +```js +if(sentence){} +``` +if string has some length then we will do a split of string with space and set a variable of maxLength to 0 + +```js +if(sentence){ + let words = sentence.split(" ") + let maxLength = 0; +} +``` +then we will iterate over each word and make each word case insensitive by applying .toLowerCase() +on each word +```js +if(sentence){ + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + }); +} +``` +next we will compare the length of each word with maxLength variable. If word length is greater than maxLength variable then we will push it in an arrayOfMaxWords and store its length to maxLength variable. + +```js +if(sentence){ + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + maxLength = word.length + arrayOfMaxWords.push(word) + }); +} +``` +another use case is what if the current word length and maxLength is same then we will push it in to a same array. So here we go for it. +```js +if(sentence){ + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + if (word.length > maxLength) { + maxLength = word.length + arrayOfMaxWords.push(word) + } + else if (word.length === maxLength) { + arrayOfMaxWords.push(word) + } + }); +} +``` +now what if the the same word arrive more than 1 time in a string then we will check for it too. So we will update our second condition i.e + +```js +if(sentence){ + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + if (word.length > maxLength) { + maxLength = word.length + arrayOfMaxWords.push(word) + } + else if (word.length === maxLength && word !== arrayOfMaxWords[0]) { + arrayOfMaxWords.push(word) + } + }); +} +``` +Now the last thing is that every time our word length is greater than maxLength then we have to clear the previous array. So the final code is : + +```js +function longestWords(sentence) { + let arrayOfMaxWords = [] + if (sentence) { + let words = sentence.split(" ") + let maxLength = 0; + words.forEach((_word) => { + let word = _word.toLowerCase(); + if (word.length > maxLength) { + maxLength = word.length + arrayOfMaxWords = [] + arrayOfMaxWords.push(word) + } + else if (word.length === maxLength && word !== arrayOfMaxWords[0]) { + arrayOfMaxWords.push(word) + } + }); + } + return arrayOfMaxWords; +} +``` + +By calling function with these cases we will get the below outpus: + +```js +longestWords("You are just an old antidisestablishmentarian") // [ 'antidisestablishmentarian' ] +longestWords("I gave a present to my parents")// [ 'present', 'parents' ] +longestWords("Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo")// [ 'buffalo' ] +``` +



From 10f52a63c00e87b4b69fe504efdfa58228863397 Mon Sep 17 00:00:00 2001 From: Muhammad Shoaib Date: Tue, 6 Oct 2020 19:40:02 +0500 Subject: [PATCH 2/2] removed console log --- Longest Words In String/longest_words_in_string.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Longest Words In String/longest_words_in_string.js b/Longest Words In String/longest_words_in_string.js index 0423819..73f9c7a 100644 --- a/Longest Words In String/longest_words_in_string.js +++ b/Longest Words In String/longest_words_in_string.js @@ -16,7 +16,4 @@ function longestWords(sentence) { }); } return arrayOfMaxWords; -} -console.log(longestWords("You are just an old antidisestablishmentarian")) -console.log(longestWords("I gave a present to my parents")) -console.log(longestWords("Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo")) \ No newline at end of file +} \ No newline at end of file