Skip to content

Instantly share code, notes, and snippets.

@gvt
Created December 18, 2013 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvt/8027965 to your computer and use it in GitHub Desktop.
Save gvt/8027965 to your computer and use it in GitHub Desktop.
pig latin translator
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