Skip to content

Commit c55a874

Browse files
authored
Added new method to find the length of longest word.
1 parent a3cbd33 commit c55a874

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

LongestWord.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Return the length longest word from a string
1+
// Return the length longest word from a string and ignore punctuations
22

33
//Method 1 - using split(), map() and Math.max()
44
function longestWord1(str) {
5-
let words = str.split(' ')
5+
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
66
let lengths = words.map(word => word.length)
77
return Math.max(...lengths)
88
}
99

10-
console.log(longestWord1("This is a great"))
10+
console.log(longestWord1("This is a awesome"))
1111

1212

1313
//Method 2 - using split(), sort() and length()
1414
function longestWord2(str) {
15-
let words = str.split(' ')
15+
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
1616
let sortedWords = words.sort( (a, b) => b.length - a.length)
1717
return sortedWords[0].length
1818
}
@@ -22,7 +22,7 @@ console.log(longestWord2("This is a great"))
2222

2323
//Method 3 - using split(), for loop and length()
2424
function longestWord3(str) {
25-
let words = str.split(' ')
25+
let words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
2626
let longestWord = 0
2727
for(let i = 0; i < words.length; i++) {
2828

@@ -33,29 +33,45 @@ function longestWord3(str) {
3333
return longestWord;
3434
}
3535

36-
console.log(longestWord3("This is a great"));
36+
console.log(longestWord3("This is a fanatstic"));
3737

3838

3939

4040
//Method 4 - using split(), reduce() and length()
4141
function longestWord4(str) {
42-
var longestWord = str.split(' ').reduce((longest, currentWord) => {
42+
var longestWord = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ").reduce((longest, currentWord) => {
4343
return currentWord.length > longest.length ? currentWord : longest
4444
}, "")
4545
return longestWord.length
4646
}
4747

48-
console.log(longestWord4("This is a great"));
48+
console.log(longestWord4("This is amazing "));
4949

5050

5151
//Method 5 - using split() and forEach()
5252
function longestWord5(str) {
53-
var words = str.split(" ")
53+
var words = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
5454
var longest = 0;
5555
words.forEach(word => {
5656
return longest < word.length ? longest = word.length : ''
5757
});
5858

5959
return longest;
6060
}
61-
console.log(longestWord5("This is a great"));
61+
console.log(longestWord5("This is a inspiring"));
62+
63+
//Method 6 -
64+
function longestWord6(str) {
65+
let strArray = str.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"").split(" ")
66+
let largestLength = 0
67+
for (let i= 0; i < strArray.length; i++) {
68+
if (strArray[i].length > largestLength) {
69+
largestLength = strArray[i].length
70+
}
71+
}
72+
return largestLength
73+
}
74+
75+
console.log(longestWord6("This is a incredible"))
76+
77+

0 commit comments

Comments
 (0)