-1

   

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"]));
georg
  • 211,518
  • 52
  • 313
  • 390
Royal_kid
  • 3
  • 5
  • 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 Answers4

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
  • But OP want to use `>=` for comparison – DecPK 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
  • 1
    I 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
  • 1
    Though your solution is concise and easy to read, understand and maintain. – DecPK Sep 21 '21 at 08:34
  • 1
    And I was also bored and needed something constructive to do :) – Andy Sep 21 '21 at 08:34
  • 1
    lol, thats quite dope. thank you @ HR01M8055 and @Andy. – Royal_kid Sep 21 '21 at 08:45
  • hey, lets say i dont want it to output an array? just the strings? – Royal_kid Sep 21 '21 at 09:38
  • 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
  • it does work but it seems it also added an undefined value. – Royal_kid Sep 21 '21 at 10:18
  • 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
  • Alright cool. thank you again. – Royal_kid Sep 21 '21 at 10:45
  • 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
0

You can make a couple of modifications to your code, and it will work :

  1. You are not checking the length of the longStr when comparing.
  2. You are returning from the very first iteration. Return at the end.
  3. 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
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