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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

31st Jul 2022, 3:04 PM
k gready
k gready - avatar
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.
31st Jul 2022, 6:19 PM
Alex
Alex - avatar
0
Alex I am pretty new to coding.can you do an example?
31st Jul 2022, 7:02 PM
k gready
k gready - avatar
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
31st Jul 2022, 7:26 PM
Alex
Alex - avatar
0
What do you mean negate with a? Can you show how you do it in my code? I just don't understand..
31st Jul 2022, 7:43 PM
k gready
k gready - avatar
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
31st Jul 2022, 8:46 PM
k gready
k gready - avatar
0
k gready That's strange. What's the full input for these test cases?
2nd Aug 2022, 11:36 AM
Alex
Alex - avatar