|
1 |
| -/************************************************************************* |
2 |
| -* * |
3 |
| -* Using the JavaScript language, have the function LongestWord(sen) * |
4 |
| -* take the sen parameter being passed and return the largest word in * |
5 |
| -* the string. If there are two or more words that are the same length, * |
6 |
| -* return the first word from the string with that length. Ignore * |
7 |
| -* punctuation and assume sen will not be empty. * |
8 |
| -* * |
9 |
| -*************************************************************************/ |
10 |
| - |
| 1 | +/*************************************************************************************** |
| 2 | +* * |
| 3 | +* CODERBYTE BEGINNER CHALLENGE * |
| 4 | +* * |
| 5 | +* Longest Word * |
| 6 | +* Using the JavaScript language, have the function LongestWord(sen) take the sen * |
| 7 | +* parameter being passed and return the largest word in the string. If there are * |
| 8 | +* two or more words that are the same length, return the first word from the string * |
| 9 | +* with that length. Ignore punctuation and assume sen will not be empty. * |
| 10 | +* * |
| 11 | +* When working on the solution you have to be aware of the directions that says to * |
| 12 | +* ignore punctuation. That means we need to strip out everything in the string * |
| 13 | +* except for letters a-z and space. Once removed I am going to use the sort() * |
| 14 | +* function for Array to sort each word by length in descending order. The first * |
| 15 | +* entry in the array is the longest word. * |
| 16 | +* 1) Remove anything in string that is not a-z, A-Z or blank * |
| 17 | +* 2) Convert String to Array * |
| 18 | +* 3) Sort Array in descending order based on length of word * |
| 19 | +* 4) First entry in sorted array is solution * |
| 20 | +* * |
| 21 | +***************************************************************************************/ |
| 22 | + |
11 | 23 | function LongestWord(sen) {
|
12 | 24 |
|
13 | 25 | var arr = sen.replace(/[^a-zA-Z ]/g,"").split(" ");
|
|
0 commit comments