A function that takes in a string and returns how many words it has. like so:
wordCount("Bob the tomato") => 3
ignore spelling
wordCount("B B H JK") => 4
function wordCount(str) {
var words = 1;
for(var i = 1; i < str.length; i++) {
if(str[i] == " " && str[i - 1] !== " "&& str[i + 1] !== " ") {
words++;
}
}
return words;
}
// TODO: Replace examples and use TDD development by writing your own tests
// These are some CW specific test methods available:
// Test.expect(boolean, [optional] message)
// Test.assertEquals(actual, expected, [optional] message)
// Test.assertSimilar(actual, expected, [optional] message)
// Test.assertNotEquals(actual, expected, [optional] message)
// NodeJS assert is also automatically required for you.
// assert(true)
// assert.strictEqual({a: 1}, {a: 1})
// assert.deepEqual({a: [{b: 1}]}, {a: [{b: 1}]})
// You can also use Chai (http://chaijs.com/) by requiring it yourself
// var expect = require("chai").expect;
// var assert = require("chai").assert;
// require("chai").should();
Test.assertEquals(wordCount(" v"), 1);
Test.assertEquals(wordCount("Word"), 1);
Test.assertEquals(wordCount("Wo r d"), 3);
Test.assertEquals(wordCount("Bob the t-rex"), 3);