-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestWord.js
77 lines (57 loc) · 2.1 KB
/
LongestWord.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
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
// Return the length longest word from a string and ignore punctuations
//Method 1 - using split(), map() and Math.max()
function longestWord1(str) {
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
let lengths = words.map(word => word.length)
return Math.max(...lengths)
}
console.log(longestWord1("This is a awesome"))
//Method 2 - using split(), sort() and length()
function longestWord2(str) {
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
let sortedWords = words.sort( (a, b) => b.length - a.length)
return sortedWords[0].length
}
console.log(longestWord2("This is a great"))
//Method 3 - using split(), for loop and length()
function longestWord3(str) {
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
let longestWord = 0
for(let i = 0; i < words.length; i++) {
if(words[i].length > longestWord) {
longestWord = words[i].length
}
}
return longestWord;
}
console.log(longestWord3("This is a fanatstic"));
//Method 4 - using split(), reduce() and length()
function longestWord4(str) {
var longestWord = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ").reduce((longest, currentWord) => {
return currentWord.length > longest.length ? currentWord : longest
}, "")
return longestWord.length
}
console.log(longestWord4("This is amazing "));
//Method 5 - using split() and forEach()
function longestWord5(str) {
var words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
var longest = 0;
words.forEach(word => {
return longest < word.length ? longest = word.length : ''
});
return longest;
}
console.log(longestWord5("This is a inspiring"));
//Method 6 -
function longestWord6(str) {
let strArray = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
let largestLength = 0
for (let i= 0; i < strArray.length; i++) {
if (strArray[i].length > largestLength) {
largestLength = strArray[i].length
}
}
return largestLength
}
console.log(longestWord6("This is a incredible"))