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;
};
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;
};
Share
Improve this question
asked Jan 14, 2014 at 6:11
jstonejstone
4453 gold badges8 silver badges19 bronze badges
6
- strArr was ["hello", "world", "after", "all"] => after , but after is 4th largest, after "all", "hello", "world", right? – Andrew Commented Jan 14, 2014 at 6:47
- Can you describe the condition returning the third item from the sorted array violates? – Maus Commented Jan 14, 2014 at 6:50
- @Andrew - ya that is correct, although that is not what the problem states. it says that 'after' is the 'third largest' – jstone Commented Jan 14, 2014 at 7:01
-
@Maus - if you look at
arr3
the patter is not the same as'jenn'
should be the third largest, even though she may be 4th in the list. – jstone Commented Jan 14, 2014 at 7:03 - @jstone, you mean you need to find the in strs with the largest length, which will be ["hello", "world", "after"], and pick the 3rd one. But if so, what about there's only one largest str – Andrew Commented Jan 14, 2014 at 7:05
4 Answers
Reset to default 4You 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!
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
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];
};
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!!