0
Hello, can you help me fix my code? It doesn't work fully for some cases that have a number in the longest word of the sentence
longestWord function receive 1 argument 'sentence' the function return the longest word in the sentence, only letters will be counted (numbers and special signs will be ignored) https://code.sololearn.com/Wr3XVL2SPHZz/?ref=app
7 Answers
0
If there are numbers inside the words I'd split them by an empty string instead of using match. This will create an array with the numbers still in it.
Then in the sort you need to ignore digits and special characters by using replace with a regex pattern, that only matches non-characters and an empty string as replace value. Put the replace between the values (a,b) and length.
0
Alex I am pretty new to coding.can you do an example?
0
split splits a string by a delimiter you pass in and returns an array with the substrings.
let str = 'abc,def,1234';
var arr = str.split(';')
result:
arr -> ['abc', 'def', '1234']
In your case you need to split by an empty space ' '.
Replace replaces a substring of a string with another string and returns the new string.
let str = abcdefg;
str = str.replace('c', 'X')
result:
str -> 'abXdefg'
You can also pass a regex pattern. You can use the one you already made, but need to negate it with a ^:
b.replace(/[^A-Za-z]/g, '').length
0
What do you mean negate with a? Can you show how you do it in my code? I just don't understand..
0
Thank you, but it did work fully.it only work on the first and second test but the third and the fourth
Those are the longest word in every test:
Test 1:Bootcamper
Test 2:cohen
Test 3:javascr1pt
Test 4:a1b2c3
0
k gready That's strange. What's the full input for these test cases?