Starting the kata

The first thing you will do for the kata is write an ItExists test. Again, these types of test help to get the ball rolling and prevent writer's block. You will replace the code for checking the framework with the following.

Red phase; write a failing test that expects an isPalindrome function to exist: 

describe('Is Palindrome', () => {
it('exists', () => {
expect(isPalindrome).to.exist;
});
});

Verify that this test fails. It's important to see failure before moving on to making the test pass. This will help confirm that your test setup is working properly.

And now the green phase; make the test pass. Define an isPalindrome function and run the test once more to see it pass.

function isPalindrome() {
}

For the next test, we want to think of the simplest test case that would produce a result. Again, we are skipping bad data issues. The simplest test case we can think of that would produce a result is a single letter. For the definition of palindrome that you will be using for these tests, a single letter is a palindrome. Add the following test under the previous one:

it('a single letter is a palindrome', () => {
// arrange
const value = 'a';

// act
const result = isPalindrome(value);
  // assert
expect(result).to.be.true;
});

Now, to make it pass:

function isPalindrome() {
return true;
}

Now you have a passing test, but you are always returning true. You want the next test to fail when you write it. So, you should write a test for when the value passed in is not a palindrome. The simplest non-palindrome would be two letters that are not the same:

it('two non-matching letters is not a palindrome', () => {
// arrange
const value = 'at';

// act
const result = isPalindrome(value);

// assert
expect(result).to.be.false;
});

Now, make it pass:

function isPalindrome(value) {
if(value.length === 1) {
return true;
}

return false;
}

Okay, so now you only return true single letters. This opens us up for our next test, flipping back to something that is a palindrome; write a test for two letters that are the same:

it('two matching letters are a palindrome', () => {
// arrange
const value = 'oo';

// act
const result = isPalindrome(value);

// assert
expect(result).to.be.true;
});

Now, to make it pass:

function isPalindrome(value) {
if(value.length === 1) {
return true;
}

if(value.length === 2 && value[0] === value[1]) {
return true;
}

return false;
}

The next test is to have a three-letter word that is a palindrome. Currently, this should fail:

it('three letter palindrome', () => {
// arrange
const value = 'mom';

// act
const result = isPalindrome(value);

// assert
expect(result).to.be.true;
});

To make this test pass, think about what you have so far. One algorithm for checking a palindrome is to simply start on the outsides and check the two outermost letters. If those two letters are a match, then move in one letter on each side. Repeat this check until you get to the center of the word or phrase. If the center is one letter, then it's a palindrome; otherwise, check if the two center-most characters are a match. Let's try this concept out by using recursion to make the latest test pass:

function isPalindrome(value) {
if(value.length === 1) {
return true;
}

if(value.length === 2 && value[0] === value[1]) {
return true;
}

  if(value[0] === value[value.length -1]) {
return isPalindrome(value.substring(1, value.length - 1));
}

return false;
}

We now need to check if a four-letter palindrome will pass:

it('four letter palindrome', () => {
// arrange
const value = 'abba';

// act
const result = isPalindrome(value);

// assert
expect(result).to.be.true;
});

It passes; excellent! We will end this code kata with two exercises for you. The first exercise is to add a test for "a man a plan a canal panama" and make it pass. The second exercise is to refactor the code for isPalindrome. While this is a small function, it could still do with some tidying up, and potentially some optimizations. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.222.175