let printLongestStrings =(array) =>{
let longStr = "";
for(let i = 0; i < array.length; i++){
if(array[i].length >= longStr.length){
longStr += array[i];
}
}
return longStr;
};
console.log(printLongestStrings(["time","ticking","bomb","countup"]));
Asked
Active
Viewed 71 times
-1
-
1) You're comparing a string's `.length` to a string… 2) You're concatenating to `longStr`, meaning it'll get longer and longer… 3) You're `return`ing on the first iteration, so it'll never try more than one… – deceze Sep 21 '21 at 08:07
4 Answers
3
1) Early returning should be there. You should return longStr
at last.
return longStr;
2) You should compare array[i].length
with longStr.length
not with longStr
array[i].length >= longStr.length
3) You should assign the largest string to longStr
. You shouldn't append it in longStr
.
longStr = array[i].length >= longStr.length ? array[i] : longStr;
let printLongestStrings = (array) => {
let longStr = "";
for (let i = 0; i < array.length; i++) {
longStr = array[i].length >= longStr.length ? array[i] : longStr;
}
return longStr;
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));
ALTERNATE SOLUTION: You can achieve the same result using reduce
let printLongestStrings = (array) => {
return array.reduce((acc, curr) => (curr.length >= acc.length ? curr : acc));
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));
If you want array of largest length string
let printLongestStrings = (array) => {
let map = new Map(),
largestLength = 0;
array.forEach((s) => {
if (s.length >= largestLength) {
largestLength = s.length;
map.has(s.length) ? map.get(s.length).push(s) : map.set(s.length, [s]);
}
});
return [...map.get(largestLength)];
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));

DecPK
- 24,537
- 6
- 26
- 42
-
Shouldn't "ticking" be included in the output? It has the same length as "countup". – Andy Sep 21 '21 at 08:17
-
i get it but it only prints one longest string and there are two longest strings in the array. – Royal_kid Sep 21 '21 at 08:17
-
-
yea, it should be included because its of the same length with countup. – Royal_kid Sep 21 '21 at 08:18
-
So @NATHANIEL You want array of same largest length strings. Is that right? – DecPK Sep 21 '21 at 08:19
-
@NATHANIEL Now you will get an array of largest same length. Have a look – DecPK Sep 21 '21 at 08:27
-
[I wrote some code too](https://jsfiddle.net/Logjqnm2/1/) which you might find interesting. @NATHANIEL. – Andy Sep 21 '21 at 08:30
-
1I came up with same solution @Andy but I think It will take 2 round(loop) to get the result. But I want to achieve the result in one go that's why I've used `forEach` and `Map` – DecPK Sep 21 '21 at 08:33
-
1Though your solution is concise and easy to read, understand and maintain. – DecPK Sep 21 '21 at 08:34
-
1
-
1
-
-
then use `[...map.get(largestLength)].join(", ")` . Use the [join](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) – DecPK Sep 21 '21 at 09:45
-
not to separate it by a comma but to print each string on a new line? – Royal_kid Sep 21 '21 at 09:50
-
`return [...map.get(largestLength)].forEach((s) => console.log(s));` This would work – DecPK Sep 21 '21 at 09:55
-
-
That's because you are logging out the output of `printLongestStrings` function . Just remove the console.log and run as `printLongestStrings(["time", "ticking", "bomb", "countup"]);` – DecPK Sep 21 '21 at 10:22
-
awesome. :) okay, do you think you can write the code without built in methods? – Royal_kid Sep 21 '21 at 10:35
-
@NATHANIEL If you have another question then please ask again. It is not advised to ask question inside another quesiton in a comment. Hope you understand – DecPK Sep 21 '21 at 10:43
-
-
Would you mind If you can [upvote](https://stackoverflow.com/help/privileges/vote-up#:~:text=How%20do%20I%20vote%20up,arrow%20to%20undo%20a%20downvote.) and [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) my answer if it solved your issue? – DecPK Sep 21 '21 at 11:02
1
You're returning before you've checked all the strings
let printLongestStrings = (array) => {
let longStr = "";
for (let i = 0; i < array.length; i++) {
if(array[i].length >= longStr.length)
longStr = array[i];
}
return longStr;
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));
The above will print the last longest string (ie, if more than 1 input is the longest length it will just output the last)
If yo want all of the strings this length the below would work
let printLongestStrings = (array) => {
let longLen = 0;
let longStr = []
for (let i = 0; i < array.length; i++) {
if(array[i].length == longLen){
longStr.push(array[i])
}
else if(array[i].length > longLen){
longStr = [];
longLen = array[i].length;
longStr.push(array[i])
}
}
return longStr;
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));

Jamiec
- 133,658
- 13
- 134
- 193
-
i tried using that but it only prints the last string or just one long string, the ideal outcome is to print all longest strings. – Royal_kid Sep 21 '21 at 08:11
-
-
-
-
cool, thank you again... lets say i dont want it to output an array? – Royal_kid Sep 21 '21 at 09:36
-
@NATHANIEL then what _do_ you want to output it as? You could always call `.join(",")` on the result to get a comma separated string – Jamiec Sep 21 '21 at 09:41
-
-
-
okay awesome... quick question, after the else if statement, why is it neccessary to add longStr = []; again even after declaring it the first time? – Royal_kid Sep 21 '21 at 11:59
-
@NATHANIEL you've found a longer string than previously found, so it is essentially clearing the array of "longest strings" by creating a new empty array. – Jamiec Sep 21 '21 at 12:01
0
You can make a couple of modifications to your code, and it will work :
- You are not checking the length of the
longStr
when comparing. - You are returning from the very first iteration. Return at the end.
- Assign the correct value, based on your comparison of lengths. The one whose
.length
is greater.
let printLongestStrings = (array) => {
let longStr = "";
for (let i = 0; i < array.length; i++) {
longStr = (array[i].length >= longStr.length) ? array[i] : longStr;
}
return longStr;
};
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));

Tushar Shahi
- 16,452
- 1
- 18
- 39
-
okay but there are two strings ofthe same length but it only prints one? – Royal_kid Sep 21 '21 at 08:14
0
This gives ticking
:
let printLongestStrings = array => {
const sorted = array.sort((a, b) => b.length - a.length);
let i = 0;
const returned = [sorted[i]];
while(sorted[i].length == sorted[i+1].length){
returned.push(sorted[++i]);
}
return returned;
}
console.log(printLongestStrings(["time", "ticking", "bomb", "countup"]));

Yevhenii Shlapak
- 588
- 2
- 4
- 13
-
is it possible to print both ticking and countup because they are of the same length – Royal_kid Sep 21 '21 at 08:23
-