function wordCount(str) { // error on no matches. // also, are there any other characters besides \w and - that shouldn't break words? return str.match(/[\w-]+/g).length; }
- function wordCount(str) {
return str.split(/\s/g).filter(x => x.length > 0).length;- // error on no matches.
- // also, are there any other characters besides \w and - that shouldn't break words?
- return str.match(/[\w-]+/g).length;
- }
// 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(" "), 1); Test.assertEquals(wordCount(" 'Twas brillig and the slithy toves; did gyre and gimble in the wabe"), 13); Test.assertEquals(wordCount("Word"), 1); Test.assertEquals(wordCount("Wo r d"), 3); Test.assertEquals(wordCount("Bob the t-rex"), 3);
- // 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(" "), 1);
- Test.assertEquals(wordCount(" 'Twas brillig and the slithy toves; did gyre and gimble in the wabe"), 13);
- Test.assertEquals(wordCount("Word"), 1);
- Test.assertEquals(wordCount("Wo r d"), 3);
- Test.assertEquals(wordCount("Bob the t-rex"), 3);