Q: I am tryng to print out all longst strngs and what i have managed to do is just print the first 2 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"]));



A: 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"]));



A: 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"]));



A: 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"]));



A: 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"]));

