1
- // Return the length longest word from a string
1
+ // Return the length longest word from a string and ignore punctuations
2
2
3
3
//Method 1 - using split(), map() and Math.max()
4
4
function longestWord1 ( str ) {
5
- let words = str . split ( ' ' )
5
+ let words = str . replace ( / [ . , \/ # ! $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g , "" ) . split ( " " )
6
6
let lengths = words . map ( word => word . length )
7
7
return Math . max ( ...lengths )
8
8
}
9
9
10
- console . log ( longestWord1 ( "This is a great " ) )
10
+ console . log ( longestWord1 ( "This is a awesome " ) )
11
11
12
12
13
13
//Method 2 - using split(), sort() and length()
14
14
function longestWord2 ( str ) {
15
- let words = str . split ( ' ' )
15
+ let words = str . replace ( / [ . , \/ # ! $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g , "" ) . split ( " " )
16
16
let sortedWords = words . sort ( ( a , b ) => b . length - a . length )
17
17
return sortedWords [ 0 ] . length
18
18
}
@@ -22,7 +22,7 @@ console.log(longestWord2("This is a great"))
22
22
23
23
//Method 3 - using split(), for loop and length()
24
24
function longestWord3 ( str ) {
25
- let words = str . split ( ' ' )
25
+ let words = str . replace ( / [ . , \/ # ! $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g , "" ) . split ( " " )
26
26
let longestWord = 0
27
27
for ( let i = 0 ; i < words . length ; i ++ ) {
28
28
@@ -33,29 +33,45 @@ function longestWord3(str) {
33
33
return longestWord ;
34
34
}
35
35
36
- console . log ( longestWord3 ( "This is a great " ) ) ;
36
+ console . log ( longestWord3 ( "This is a fanatstic " ) ) ;
37
37
38
38
39
39
40
40
//Method 4 - using split(), reduce() and length()
41
41
function longestWord4 ( str ) {
42
- var longestWord = str . split ( ' ' ) . reduce ( ( longest , currentWord ) => {
42
+ var longestWord = str . replace ( / [ . , \/ # ! $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g , "" ) . split ( " " ) . reduce ( ( longest , currentWord ) => {
43
43
return currentWord . length > longest . length ? currentWord : longest
44
44
} , "" )
45
45
return longestWord . length
46
46
}
47
47
48
- console . log ( longestWord4 ( "This is a great " ) ) ;
48
+ console . log ( longestWord4 ( "This is amazing " ) ) ;
49
49
50
50
51
51
//Method 5 - using split() and forEach()
52
52
function longestWord5 ( str ) {
53
- var words = str . split ( " " )
53
+ var words = str . replace ( / [ . , \/ # ! $ % \^ & \* ; : { } = \- _ ` ~ ( ) ] / g , "" ) . split ( " " )
54
54
var longest = 0 ;
55
55
words . forEach ( word => {
56
56
return longest < word . length ? longest = word . length : ''
57
57
} ) ;
58
58
59
59
return longest ;
60
60
}
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