-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-word.js
41 lines (29 loc) · 1.28 KB
/
longest-word.js
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
35
36
37
38
39
40
41
/*
Challenge from: https://www.coderbyte.com/
Description: Have the function LongestWord(sen) take the sen parameter being passed and return the largest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty.
Eg:
LongestWord('I ride bike!!!!!') => 'ride'
*/
// => solution 1
// function LongestWord(sen) {
// const strippedSen = sen.replace(/[^a-zA-Z0-9 ]/g, "");
// const strippedSenArray = strippedSen.split(" ");
// if (strippedSenArray.length === 0) return null;
// if (strippedSenArray.length === 1) return strippedSenArray[0];
// let maxLengthWord = "";
// // eslint-disable-next-line no-restricted-syntax
// for (const arr of strippedSenArray) {
// if (arr.length > maxLengthWord.length) maxLengthWord = arr;
// }
// return maxLengthWord;
// }
// => solution 2
function LongestWord(sen) {
const strippedSen = sen.replace(/[^a-zA-Z0-9 ]/g, "");
const strippedSenArray = strippedSen.split(" ");
if (strippedSenArray.length === 0) return null;
if (strippedSenArray.length === 1) return strippedSenArray[0];
const sortedArray = strippedSenArray.sort((a, b) => b.length - a.length);
return sortedArray[0];
}
export default LongestWord;