Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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"]));

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!