#TDD Pig Latin
A translator from English to Pig Latin, written javascript.
Tests run from the command line using node. If you don't have node, you can get it [here](http://nodejs.org/ here).
Run the tests:
node test
#TDD Pig Latin
A translator from English to Pig Latin, written javascript.
Tests run from the command line using node. If you don't have node, you can get it [here](http://nodejs.org/ here).
Run the tests:
node test
function ay(str) { | |
return str + 'ay'; | |
} | |
function translateWord(str) { | |
if (/^[aeiou]/i.test(str)) { | |
return ay(str); | |
} | |
if (/^[A-Z]/.test(str)) { | |
return ay(str[1].toUpperCase() + str.substring(2) + str[0].toLowerCase()); | |
} | |
return ay(str.substring(1) + str[0]); | |
} | |
exports.translate = function (str) { | |
return str.replace(/\b\w+\b/g, translateWord); | |
}; |
var assert = require('assert'); | |
var pigLatin = require('./piglatin.js'); | |
function assertCorrectTranslation(input, expected) { | |
try { | |
var pig = pigLatin.translate(input); | |
assert.equal(pig, expected, '"'+input+'" should translate to "'+expected+'", but was "'+pig+'"'); | |
console.log('PASSED'); | |
} catch (er) { | |
console.log('FAIL!!! '+er.message); | |
} | |
} | |
assertCorrectTranslation('hello', 'ellohay'); | |
assertCorrectTranslation('hello world', 'ellohay orldway'); | |
assertCorrectTranslation('Hello World', 'Ellohay Orldway'); | |
assertCorrectTranslation('Hello, World!!', 'Ellohay, Orldway!!'); | |
assertCorrectTranslation('I eat apples', 'Iay eatay applesay'); |