Created
December 18, 2013 19:09
-
-
Save gvt/8027965 to your computer and use it in GitHub Desktop.
pig latin translator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert = require "assert" | |
pigLatin = (s) -> | |
words = s.split " " | |
words.map(doTranslate).join " " | |
doTranslate = (w) -> | |
latin = "ay" | |
remainingWord = w.substring(1) | |
firstChar = w[0] | |
if /[A-Z]/.test firstChar | |
firstChar = w[0].toLowerCase() | |
firstWord = remainingWord[0].toUpperCase() + remainingWord.substring(1) | |
else | |
firstWord = remainingWord | |
firstWord + firstChar + latin | |
assert.equal pigLatin("hello"), "ellohay" | |
assert.equal pigLatin("hello world"), "ellohay orldway" | |
assert.equal pigLatin("Hello world"), "Ellohay orldway" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment