Back to Freecodecamp

Challenge 85: Word Counter

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68ee9e3066cfd4eb2328e8a4.md

latest1.1 KB
Original Source

--description--

Given a sentence string, return the number of words that are in the sentence.

  • Words are any sequence of non-space characters and are separated by a single space.

--hints--

countWords("Hello world") should return 2.

js
assert.equal(countWords("Hello world"), 2);

countWords("The quick brown fox jumps over the lazy dog.") should return 9.

js
assert.equal(countWords("The quick brown fox jumps over the lazy dog."), 9);

countWords("I like coding challenges!") should return 4.

js
assert.equal(countWords("I like coding challenges!"), 4);

countWords("Complete the challenge in JavaScript and Python.") should return 7.

js
assert.equal(countWords("Complete the challenge in JavaScript and Python."), 7);

countWords("The missing semi-colon crashed the entire internet.") should return 7.

js
assert.equal(countWords("The missing semi-colon crashed the entire internet."), 7);

--seed--

--seed-contents--

js
function countWords(sentence) {

  return sentence;
}

--solutions--

js
function countWords(sentence) {

  return sentence.split(' ').length;
}