Create a function disp3char which takes a string as a parameter and displays all the 3 letter words of the sentence. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Create a function disp3char which takes a string as a parameter and displays all the 3 letter words of the sentence.

example: entered string is Hii rahul you are a nice person. output: Hii you are

26th Jan 2018, 1:31 PM
Harsh
2 Answers
+ 1
Javascript: function disp3char(string){ string = string || "There was no input"; const allWords = string.split(" "), words = allWords.filter(function(element){ return element.length === 3; }); console.log(words.join(" ")); } Warning: this may throw errors if there are less than 3 words.
26th Jan 2018, 1:43 PM
Draphar
0
JavaScript: const disp3char = s => s.split(' ').filter(x => x.length === 3).join(' '); RegEx (all 3-letter words in group 1): /(?:(?:(\w{3})(?:\s+|$))|(?:\w+\s*?))/img The RegEx works on regex101.com but not in the JS regex engine.
26th Jan 2018, 2:22 PM
SplittyDev
SplittyDev - avatar