Number of Valid Words in a Sentence LeetCode solution in JavaScript

1
/**
2
* @param {string} sentence
3
* @return {number}
4
*/
5
var countValidWords = function (sentence) {
6
let list = sentence.split(" ");
7
let filtered = list.filter((s) => {
8
if (/\d/.test(s) || s === "") return false; //removes anything with numbers or is blank
9
if (/^[!,.]$/.test(s)) return true; //punctuation only
10
if (/^\w+[!,.]?$/.test(s)) return true; //word + optional punctuation
11
if (/^\w+[-]?\w+[!,.]?$/.test(s)) return true; //word + optional hypen + word + optional punctuation
12
return false;
13
});
14

15
return filtered.length;
16
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0