jstone
jstone

Reputation: 445

find third largest word in array - CoderByte - javascript

I am new to JS and am trying out CoderByte. The problem below I have gone around in circles trying to figure it out. I am stuck on how to get to the third largest word. Thanks in advance for the guidance.

Problem from CoderByte

Using the JavaScript language, have the function ThirdGreatest(strArr) take the array of strings stored in strArr and return the third largest word within in. So for example: if strArr is ["hello", "world", "before", "all"] your output should be world because "before" is 6 letters long, and "hello" and "world" are both 5, but the output should be world because it appeared as the last 5 letter word in the array. If strArr was ["hello", "world", "after", "all"] the output should be after because the first three words are all 5 letters long, so return the last one. The array will have at least three strings and each string will only contain letters.

var arr = ["hello", "world", "before", "all"]; => world   sorted = ["all", "hello", "world", "before"]
var arr2 = ["hello", "world", "after", "all"]; => after   sorted = ["all", "hello", "world", "after"]
var arr3 = ['all', 'mary', 'jenn', 'frank', 'marshall', 'bob', 'sam', 'may']; => sorted = ["marshall", "frank", "mary", "jenn", "all", "bob", "sam", "may"]

var thirdGreatest = function (arr) {
    var arr2 = arr.sort(function(a,b){ return b.length - a.length});
    return arr2;
};

Upvotes: 2

Views: 8960

Answers (4)

piapeti
piapeti

Reputation: 1

 public static string ArrayChallenge(string[] strArr) {
 
  
   string lo="";
    string se="";
string th="";
for(int idx=0  ;idx<  strArr.Length;idx++){
string crr = strArr[idx];
if(crr.Length > lo.Length){
th=se;se=lo;lo=crr;
}
 else if(crr.Length >se.Length){th=se;se=crr;}
 else if(crr.Length>=th.Length){th=crr;}

}
     return  th;

  }

//considering same array if we check then lo=before and th=world

//sorry return shd be th!!

Upvotes: 0

virtualnobi
virtualnobi

Reputation: 1180

Sorting is expensive, and it might change the order of words of equal length. Additionally, we're only interested in the length of words, not in their alphabetical order.

This piece of code returns the correct results on every case given at CoderByte:

function ThirdGreatest(strArr) { 
  longest = '';
  second = '';
  third = '';
  for (idx in strArr) {
    current = strArr[idx];
    if (current.length > longest.length) {
      third = second;
      second = longest;
      longest = current;
    } else if (current.length > second.length) {
      third = second;
      second = current;
    } else if (current.length >= third.length) {
      third = current;
    }
  }
  return third;          
}

It keeps the longest three words seen so far, and if it encounters a word longer than any of the three, it pushes the shorter ones out.

In the final else if statement, making ">" into >=" ensures that the third longest string will always be the latest string in the array if there are multiple with the same length

Upvotes: 2

THEJENSMAN
THEJENSMAN

Reputation: 91

Why not just sort it then get the third item of the array?

var thirdGreatest = function (arr) {
    // sorts the array
    var arr2 = arr.sort();

    // gets the third item (third largest)
    return arr2[2];
};

Upvotes: 0

ColinE
ColinE

Reputation: 70142

You are almost there, one sorted, just return the third item from the array:

var thirdGreatest = function (arr) {
    var arr2 = arr.sort(function(a,b){ return b.length - a.length});
    return arr2[2];
};

Remembering that arrays are zero indexed!

Upvotes: 4

Related Questions