-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_the_Longest_Word_in_a_String.js
More file actions
75 lines (63 loc) · 2.52 KB
/
Copy pathFind_the_Longest_Word_in_a_String.js
File metadata and controls
75 lines (63 loc) · 2.52 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const findLongestWord = s => Math.max(...s.replace(/[^A-Z0-9 -]/gi, "").split(" ").map(w => w.length));
/*
const findLongestWord = (str) => str.replace(/[^A-Z0-9 -]/gi, "").split(" ").reduce((a, b) => b.length > a ? b.length : a, 0);
*/
/*
function findLongestWord(str) {
return str.replace(/[^A-Z0-9 -]/gi, "").split(" ").reduce((a, b) => b.length > a ? b.length : a, 0);
}
On October 7, 2016, I forgot that I had the above code and someone asked in the FreeCodeCamp/HelpJavaScript room
if this challenge could be solved with reduce. I wrote this:
function findLongestWord(str) {
return str.split(" ").reduce(function (pre, curr) {
if (curr.length > pre) {
return curr.length;
}
else {
return pre;
} }, 0);
}
*/
/* Two different refactors below:
function findLongestWord (str) {
var l = 0;
str.replace(/[^A-Z0-9 -]/gi, " ").split(" ").forEach(function (x) {if (x.length > l) { l = x.length; }});
return l;
}
The first code that I wrote for this challenge:
function findLongestWord(str) {
str.replace(/[\W_]/g, ""); // Sanitize the string to remove comas.
var array = str.split(" "); // Split the string into an array
var elementSize = 0; // Variable for storing the size of longest item in array.
for (var i = 0; i < array.length; i++) {
if (elementSize < array[i].length) { // If the element size is less than item length
elementSize = array[i].length; // set the elementSize to the item length in the array.
}
}
// return str.length;
return elementSize;
}
*/
findLongestWord("The quick brown fox jumped over the lazy dog");
// should return a number.
findLongestWord("The quick brown fox jumped over the lazy dog");
// should return 6.
findLongestWord("May the force be with you");
// should return 5.
findLongestWord("Google do a barrel roll");
// should return 6.
findLongestWord("What is the average airspeed velocity of an unladen swallow");
// should return 8.
findLongestWord("What if we try a super-long word such as otorhinolaryngology");
// should return 19.
/* Another option for this lesson:
function findLongestWord(str) {
var longestWord = "";
str.split(" ").forEach(function (x) {
if(x.length > longestWord.length){
longestWord = x;
}});
return longestWord.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
*/