DML School of Technology
Menu

Stretch, Strings

Count the words

The task

Count the words in a piece of text. There may be extra spaces at the start, at the end or between words, and the text may be empty.

Write a function called countWords.

Tests

CallExpectedYours
countWords("the cat sat") 3 not run
countWords(" spaced out ") 2 not run
countWords("") 0 not run
countWords("one") 1 not run

Next challenge · All challenges

solution.js

Tab indents. To leave the editor, press Escape, then Tab.

Output

Run prints what your code prints. Run the tests to check your function.