Return the middle character of the string. If the string's length is odd, return the middle character. If the string's length is even, return the middle 2 characters.
function middleCharacter(str) {
if (str.length % 2 !== 0) {
return str.slice(str.length/2, str.length/2+1);
};
return str.slice(str.length/2-1, str.length/2+1);
};
// 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();
describe("Solution", function() {
it("should return 1 letter if odd, 2 if even", function(){
Test.assertEquals(middleCharacter('hello'), 'l');
Test.assertEquals(middleCharacter('dog'), 'o');
Test.assertEquals(middleCharacter('odd'), 'd');
Test.assertEquals(middleCharacter('hello world'), ' ');
Test.assertEquals(middleCharacter('hello!'), 'll');
Test.assertEquals(middleCharacter('dogs'), 'og');
Test.assertEquals(middleCharacter('even'), 've');
Test.assertEquals(middleCharacter('helloworld'), 'ow');
Test.assertEquals(middleCharacter('a'), 'a');
Test.assertEquals(middleCharacter(''), '');
});
});