Skip to content

#104 Word Count

Adam Kubiś edited this page Oct 28, 2026 · 1 revision

104 Word Count

Create a function that takes a string and returns the word count.

The string will be a sentence.

Current source and tests | Catalog | Testing and tooling

Run this solution

From the cloned repository root, after installing dependencies:

node "Edabit/104 Word Count.js"

Assertions are kept alongside the implementation and throw on failure. A successful run exits with code 0. Run every exercise with npm test or both lint and tests with npm run check.

Current implementation and tests

This is the complete executable file, including its imports and inline test cases. Run it from the repository, not as an isolated file copied out of the wiki. Line endings and trailing whitespace are normalized; executable tokens are unchanged.

//Create a function that takes a string and returns the word count.
//The string will be a sentence.
function countWords(str) {
    return str.split(' ').length;
}
import { assertEquals } from '../testHelper.js';
assertEquals(countWords("It's high noon"), 3)
assertEquals(countWords("Is this easy mode"), 4)
assertEquals(countWords("What an easy task, right"), 5)
assertEquals(countWords("This is a test"), 4)
assertEquals(countWords("Just an example here move along"), 6)
assertEquals(countWords("How are you today?"), 4)

Clone this wiki locally