Goal: Focus on breaking your project down into small behavior examples and writing one test at a time. Make a list of specifications with input and output examples before writing any code. Also, as our projects grow in size, continue to practice good organization by keeping business logic and user interface logic distinctly separate.
Let's say we have an application that counts the number of vowels in a string. This application has tests detailing each behavior it must exhibit. However, they're out of order! Reorder the following list of tests from the simplest possible behavior to the most complex behavior with your partner. Consult with other pairs if necessary.
As you work through this warm up, keep in mind that there could be multiple starting points for the simplest possible behavior. Don't get hung up on being right!
Describe: vowelCounter(); Test: "It recognizes vowels in a multiple word sentence regardless of capitalization." Code: vowelCounter("CATS CATERED THE EVENT"); Expected Output: 7 Test: "It recognizes a single vowel in a word with multiple characters." Code: vowelCounter("cat"); Expected Output: 1 Test: "It recognizes a single vowel." Code: vowelCounter("a"); Expected Output: 1 Test: "It recognizes multiple vowels in a single word." Code: vowelCounter("cater"); Expected Output: 2 Test: "It recognizes a single vowel regardless of case." Code: vowelCounter("A"); Expected Output: 1 Test: "It recognizes all vowels in a multiple-word sentence regardless of inconsistent capitalization." Code: vowelCounter("CaTS CATEReD ThE EveNT"); Expected Output: 7 Test: "It ignores non-alphabetical characters since they can't be vowels." Code: vowelCounter("*&$92%"); Expected Output: 0 Test: "It recognizes vowels in a multiple-word sentence." Code: vowelCounter("cats catered the event"); Expected Output: 7
Write a Pig Latin translator or should we say an "igPay atinLay anslatorTray"? Read all instructions carefully before beginning.
Pig Latin is a language game that involves adding imaginary endings to real English words. In this prompt, you'll create an application that turns words into Pig Latin.
Here are the rules of Pig Latin:
For words beginning with a vowel, add "way" to the end. For Pig Latin, vowels are "a," "e," "i," "o," and "u." Don't treat "y" as a vowel. Examples: "away" becomes "awayway" and "okay" becomes "okayway."
For words beginning with one or more consonants, move all of the first consecutive consonants to the end and add "ay". Examples: "code" becomes "odecay" and "move" becomes "ovemay."
If the first consonants include "qu", move the "q" and the "u." Don't forget about words like "squeal" where "qu" doesn't come first! Examples: "quick" becomes "ickquay" while "squeal" becomes "quealsay."
Before writing any code, make a list of specs detailing each behavior your program will have. Start with the simplest possible behavior, and slowly move up in complexity.
Have an instructor check your specs before you begin coding. Ensure that each possible behavior is represented by a spec, and that they are ordered from simplest to most complex.
Place your specs (and their example inputs and outputs) in your project's README.
Now you're ready to start writing (and passing tests). Here's the process:
Describe: pigLatin() Test: "It will add 'way' to the end of words that begin with a vowel." Code: pigLatin("a"); Expected Output: "away"
Write the smallest amount of code needed to get the test passing.
Test the code in the console, REPL, or JSFiddle.
Once the test is passing, commit your code.
Repeat the cycle above for each test. Do not move onto the next test until the previous one passes.
Once you've completed your business logic, feel free to add a user interface with jQuery so you can run your application in the browser. Do not add a UI until after you've written and tested all business logic.
When you get to consonants, don't try to solve it all at once. Instead, start with an example of a word that only has one consonant. Next, try a word with two consonants. Finally, work your way up to a word with three consonants. After you've written passing tests for this code, tackle the exceptions to the rule like "qu" and "y". Then, when your application can successfully translate single words, work on translating entire sentences.
Here are a few methods that you may want to consider for solving this problem. Note that there are plenty of other ways to solve this problem, too!
Take a look at this and other documentation to gather the tools you'll need before you try to fulfill your specs.
We haven't covered regular expressions yet because we want you to primarily focus on looping. But if you want to try out regular expressions and you are comfortable with looping, now is your chance to refactor Pig Latin using regular expressions as described in the lesson Further Exploration: Introduction to Regular Expressions.
Note: Once again, do not worry about using regex if you are still trying to understand loops. You do not need to use regex on any code reviews!
Binary: Write a method to convert numbers from binary to decimal. The input should be a string, and the output an integer. Decimal is the normal system we use for counting. We start at 0, increment until we reach 9, and then reset back to 0 and add another number to the left. In binary, we also start at zero, but we only increment until we reach 1. Then we reset back to zero and add another number to the left. Write specs, implement the smallest possible behavior, and don't move onto the next test until the previous test is passing.
Here are some example of numbers in decimal and binary:
Decimal Binary 0 0 1 1 2 10 3 11 4 100 ... ...
Trinary: You get where I'm going, right?
Hexadecimal: Here's what happens after 9...
Decimal Hexadecimal ... ... 9 9 10 a 11 b 12 c 13 d 14 e 15 f 16 10 17 11 ... ...
If you get this far, write a method that takes two arguments: the number to be evaluated and the base you would like it to be evaluated in.
Lesson 24 of 34
Last updated October 12, 2021