2

I'm trying to solve the below. I don't have much practical knowledge of javascript.

I need to find the largest string given from an array. If multiple strings in the array have the same length, return the first one of the longest length.

let test13 = ['a', 'aa', 'aaa'];
let test14 = ['asdf', 'qwer', 'zxcv'];
let test15 = ['asdfasdf fdasdf a sd f', ' asdfsdf', 'asdfasdfds', 'asdfsdf', 'asdfsdaf'];
let test16 = ['ddd','dddddddd','dddd','ddddd','ddd','dd','d','d','dddddddddddd'];
3
  • 2
    What about trying a for loop first and posting it? it sounds like you're on the right track :). Commented Oct 25, 2018 at 12:20
  • 1
    You could use reduce for this. Commented Oct 25, 2018 at 12:21
  • What have you tried so far? Commented Oct 25, 2018 at 12:26

5 Answers 5

9

You can use .reduce() method to find the desired string by checking the length of each string in array and returning the one having more characters:

let test1 = ['a', 'aa', 'aaa'];
let test2 = ['asdf', 'qwer', 'zxcv'];
let test3 = ['asdfasdf fdasdf a sd f', ' asdfsdf', 'asdfasdfds', 'asdfsdf', 'asdfsdaf'];
let test4 = ['ddd','dddddddd','dddd','ddddd','ddd','dd','d','d','dddddddddddd'];

let reducer = (arr) => arr.reduce((r, c) => r.length >= c.length ? r : c);

console.log(reducer(test1));
console.log(reducer(test2));
console.log(reducer(test3));
console.log(reducer(test4));

Sign up to request clarification or add additional context in comments.

1 Comment

For empty array you should do: let reducer = (arr) => arr.reduce((r, c) => r.length >= c.length ? r : c,''); Then it'll return empty string for empty array.
0
/**
 *  get the largest string of a string array
 *  returns null if input is not array or array is empty
 *
 *  @param array string[]
 *  @returns null|object {max: string, value: string}
 */
let largest = (array) => {
  // check if array is realy an array
  if(!Array.isArray(array)){
    return null;
  }
  // check if array is empty
  if(array.length < 1) {
    return null;
  }
  let len = 0;
  let str = '';
  for(let value in array){
    if(value.length > len) {
      len = len;
      str = value;
    }
  }

  return {max: len, value: str};
};

Comments

0

let test13 = ['a', 'aa', 'aaa'];
let test14 = ['asdf', 'qwer', 'zxcv'];
let test15 = ['asdfasdf fdasdf a sd f', ' asdfsdf', 'asdfasdfds', 'asdfsdf', 'asdfsdaf'];
let test16 = ['ddd','dddddddd','dddd','ddddd','ddd','dd','d','d','dddddddddddd'];

function findLongest(array){
var length=0;
var longest="";
for(var i=0;i<array.length;i++){
 
  if(array[i].length>length){
  length=array[i].length;
  longest=array[i];
  }
}
 return "String : "+longest+" Length : "+length;
}

console.log(findLongest(test13));
console.log(findLongest(test14));
console.log(findLongest(test15));
console.log(findLongest(test16));

Apply the above logic to all array.

Comments

0
var test14 = ['asdf', 'qwere', 'zxcv3eerer'];
function getLargeString(arr) {
var largeString = ""
arr.forEach(function (element) {
    if (largeString == "")
        largeString = element;
    if (largeString.length < element.length)
        largeString = element;

});
console.log(largeString);
}

getLargeString(test14);

Comments

0

Moved to => Finding longest string in array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.