Skip to content

Commit c10a262

Browse files
committed
Added easy/third_greatest
1 parent f3ffada commit c10a262

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

easy/third_greatest.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Have the function thirdGreatest(strArr) take the array of strings stored in
3+
* strArr and return the third largest word within in. So for example: if strArr
4+
* is ["hello", "world", "before", "all"] your output should be world because
5+
* "before" is 6 letters long, and "hello" and "world" are both 5, but the
6+
* output should be world because it appeared as the last 5 letter word in the
7+
* array. If strArr was ["hello", "world", "after", "all"] the output should be
8+
* after because the first three words are all 5 letters long, so return the
9+
* last one. The array will have at least three strings and each string will
10+
* only contain letters.
11+
*
12+
* https://www.coderbyte.com/results/bhanson:Third%20Greatest:JavaScript
13+
*
14+
* @param {array} strArr
15+
* @return {string}
16+
*/
17+
function thirdGreatest(strArr) {
18+
strArr.sort((a, b) => b.length - a.length);
19+
20+
return strArr[2];
21+
}
22+
23+
module.exports = thirdGreatest;

easy/third_greatest.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const thirdGreatest = require('./third_greatest');
2+
3+
describe('thirdGreatest()', () => {
4+
test('correctly returns third larget word', () => {
5+
expect(thirdGreatest(['hello', 'world', 'before', 'all'])).toBe(
6+
'world'
7+
);
8+
9+
expect(thirdGreatest(['hello', 'world', 'after', 'all'])).toBe('after');
10+
});
11+
});

0 commit comments

Comments
 (0)