Ad

Splits the string by the spaces present and then removes any empty strings.

Code
Diff
  • function wordCount(str) {
        return str.split(" ").filter(a=>a!="").length
    }
    • function wordCount(str) {
    • return str.trim() == '' ? 0 : (str.trim().split(" ")).length;
    • function wordCount(str) {
    • return str.split(" ").filter(a=>a!="").length
    • }