File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /***************************************************************************************
2+ * *
3+ * CODERBYTE BEGINNER CHALLENGE *
4+ * *
5+ * Third Greatest *
6+ * Using the JavaScript language, have the function ThirdGreatest(strArr) take the *
7+ * array of strings stored in strArr and return the third largest word within in. *
8+ * So for example: if strArr is ["hello", "world", "before", "all"] your output should *
9+ * be world because "before" is 6 letters long, and "hello" and "world" are both 5, *
10+ * but the output should be world because it appeared as the last 5 letter word in *
11+ * the array. If strArr was ["hello", "world", "after", "all"] the output should be *
12+ * after because the first three words are all 5 letters long, so return the last one. *
13+ * The array will have at least three strings and each string will only contain *
14+ * letters. *
15+ * *
16+ * SOLUTION *
17+ * I am going to sort the array in descending order using the length of each entry. *
18+ * Once sorted the third greatest number is in array position 2. *
19+ * *
20+ * Steps for solution *
21+ * 1) Sort array in decending order based on length of entry *
22+ * 2) Return the entry in the 3rd spot *
23+ * *
24+ ***************************************************************************************/
25+
26+ function ThirdGreatest(strArr) {
27+
28+ strArr.sort(function(a, b){
29+ return b.length - a.length;
30+ });
31+
32+ return strArr[2];
33+
34+ }
You can’t perform that action at this time.
0 commit comments