Exercise Wednesday

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.

Warm Up


Pretend you have an application that counts the number of vowels in a string. This application has specs detailing each behavior it must exhibit. However, they're out of order! Reorder the following list of specs from the simplest possible behavior to the most complex behavior with your partner. Consult with other pairs, if necessary:

  • The program recognizes vowels in a multiple-word sentence, regardless of capitalization.
    • Input Example: "CATS CATERED THE EVENT"
    • Output Example: 7
  • The program recognizes a single vowel in a multiple-character word.
    • Input Example: "cat"
    • Output Example: 1
  • The program recognizes a single vowel.
    • Input Example: "a"
    • Output Example: 1
  • The program recognizes multiple vowels in a single word.
    • Input Example: "cater"
    • Output Example: 2
  • The program recognizes a single vowel, regardless of case.
    • Input Example: "A"
    • Output Example: 1
  • The program recognizes all vowels in a multiple-word sentence, regardless of inconsistent capitalization.
    • Input Example: "CaTS CATEReD ThE EveNT"
    • Output Example: 7
  • The program ignores non-alphabetical characters, since they cannot be vowels.
    • Input Example: "4%"
    • Output Example: 0
  • The program recognizes vowels in a multiple-word sentence.
    • Input Example: "cats catered the event"
    • Output Example: 7

Code


Leap Year

Follow along with the leap year lesson to build an application that identifies whether a given year is a leap year. You may choose to add a UI to the application if you wish.

Pig Latin

Write a Pig Latin translator or should we say an "igPay atinLay anslatorTray"? Read all instructions carefully before beginning.

How Pig Latin Works

First, here are the rules of Pig Latin:

  • For words beginning with a vowel, add "way" to the end.

  • For words beginning with one or more consonants, move all of the first consecutive consonants to the end, and add "ay".

  • If the first consonants include "qu", move the "u" along with the "q". Don't forget about words like "squeal" where "qu" doesn't come first!

  • For words beginning with "y", treat "y" as a consonant.

Instructions

  1. 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.

  2. Have at least two other pairs 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.

  3. 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:

  1. Write a test for the simplest possible behavior. We will get you started:
Describe: `pigLatin()`
Test: "It will add 'way' to the end of words that begin with a vowel"
Expect(pigLatin("a")).toEqual("away");
  1. Write the smallest amount of code needed to get the test passing.

  2. Test the code in the console, REPL, or JSFiddle.

  3. 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.

Helpful Hints

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.

Further Exploration

Refactor with Regular Expressions

Refactor your code for the projects above using regular expressions.

Bases

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.

Peer Code Review


  • Are there tests for each behavior?
  • Are all tests passing?
  • If business and user interface logic well-separated?
  • Does the application work as expected?

If you would like to view an example README, check out this one made by Epicodus graduates, David Wilson and Marilyn Carlin. Below is the raw version.

# Scrabble Score Checker

#### A program that allows users to input a word and determine its raw Scrabble score, assuming no special tiles. 6/1/17

#### By **Marilyn Carlin and David Wilson**

## Description

A website created with C# and HTML where a user can submit a word and determine its raw Scrabble score.


### Specs
| Spec | Input | Output |
| :-------------     | :------------- | :------------- |
| **Homepage** | User accesses localhost:5004 | Homepage with user input form |
| **Program Gathers User Input** | User input: "pants" | Output: "pants" |
| **Program Removes Spaces from User Input**| User Input: "pants pants" | Output: "pantspants" |
| **Program Removes Punctuation from User Input**| Input: "p#an^[email protected]  /p(ant%s" | Output: "pantspants" |
| **Program Assigns Characters Numeric Values** | Input: "pants" | Output: "3 1 1 1 1" |
| **Program Sums Character Values**| Input: "3 1 1 1 1" | Page Displays: 7 |

## Setup/Installation Requirements

1. To run this program, you must have a C# compiler. I use [Mono](http://www.mono-project.com).
2. Install the [Nancy](http://nancyfx.org/) framework to use the view engine. Follow the link for installation instructions.
3. Clone this repository.
4. Open the command line--I use PowerShell--and navigate into the repository. Use the command "dnx kestrel" to start the server.
5. On your browser, navigate to "localhost:5004" and enjoy!

## Known Bugs
* No known bugs at this time.

## Technologies Used
* C#
  * Nancy framework
  * Razor View Engine
  * ASP.NET Kestrel HTTP server
  * xUnit

* HTML

## Support and contact details

_Email no one with any questions, comments, or concerns._

### License

*{This software is licensed under the MIT license}*

Copyright (c) 2017 **_{Marilyn Carlin, David Wilson}_**

Lesson 28 of 34
Last updated August 10, 2020