Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor
Learn TDD With Mocha

Red to Green II

Now that we have tests that cover more than one condition, it is time to program the actual behavior that we want our .initials method to execute.

Let's think about the current state of Phrase.initials(). We have one passing test and one failing test that raises the following error when you run the test suite:

1) Phrase .initials should return the first letter of each word in a phrase:

1) AssertionError: 'JMS' == 'NM'
+ expected - actual
   - 'NM'
   + 'JMS'

This is not surprising, considering our current implementation always returns 'NM.'

To write a more complete implementation, that could get us back into the green, we could write something like this:

const Phrase = { initials(inputName) { // Create an empty array for initials const initials = []; // Create an array of strings const words = inputName.split(" "); // Iterate through the array of strings and push the first character of each to array words.forEach((word) => { initials.push(word.charAt(0)); }); // Return the initials as one string return initials.join(""); } }

This implementation will successfully pass the tests we have written. Each additional test pushes us to build a complete implementation of the .initials method.

Report a Bug
If you see a bug or any other issue with this page, please report it here.