-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathThird Greatest
34 lines (30 loc) · 2.23 KB
/
Third Greatest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/***************************************************************************************
* *
* CODERBYTE BEGINNER CHALLENGE *
* *
* Third Greatest *
* 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. *
* *
* SOLUTION *
* I am going to sort the array in descending order using the length of each entry. *
* Once sorted the third greatest number is in array position 2. *
* *
* Steps for solution *
* 1) Sort array in decending order based on length of entry *
* 2) Return the entry in the 3rd spot *
* *
***************************************************************************************/
function ThirdGreatest(strArr) {
strArr.sort(function(a, b){
return b.length - a.length;
});
return strArr[2];
}