Avatar of brunnock

brunnock's solution

to Pig Latin in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement a program that translates from English to Pig Latin.

Pig Latin is a made-up children's language that's intended to be confusing. It obeys a few simple rules (below), but when it's spoken quickly it's really difficult for non-children (and non-native speakers) to understand.

  • Rule 1: If a word begins with a vowel sound, add an "ay" sound to the end of the word.
  • Rule 2: If a word begins with a consonant sound, move it to the end of the word, and then add an "ay" sound to the end of the word.

There are a few more rules for edge cases, and there are regional variants too.

See http://en.wikipedia.org/wiki/Pig_latin for more details.


Go through the setup instructions for JavaScript to install the necessary dependencies:


Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine pig-latin.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.


The Pig Latin exercise at Test First Teaching by Ultrasaurus https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


var pigLatin = require('./pig-latin');

describe('pigLatin', function () {
  it('translates a word beginning with a', function () {

  xit('translates a word beginning with e', function () {

  xit('translates a word beginning with p', function () {

  xit('translates a word beginning with k', function () {

  xit('translates a word beginning with ch', function () {

  xit('translates a word beginning with qu', function () {

  xit('translates a word with a consonant preceding qu', function () {

  xit('translates a word beginning with th', function () {

  xit('translates a word beginning with thr', function () {

  xit('translates a word beginning with sch', function () {

  xit('translates a phrase', function () {
    expect(pigLatin.translate('quick fast run'))
      .toEqual('ickquay astfay unray');
var pigLatin = {
  translate : function(words){
      var translations=[];
      var cons='';
      var qu='';

	    if (cons = word.match(/^[^aeioqu]+/i)) 
		word = word.replace(cons,'') + cons;
	    if (qu = word.match(/^qu/i)) 
		word = word.replace(qu,'') + qu;
	    translations.push(word + 'ay');
      return translations.join(' ');


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?