Gcap Gcap - 1 month ago 27x
Javascript Question

Pig Latin Translator - JavaScript

So for my cit class I have to write a pig Latin converter program and I'm really confused on how to use arrays and strings together.
The rules for the conversion are simple, you just move the first letter of the word to the back and then add ay. ex: hell in English would be ellhay in pig Latin
I have this so far:

<form name="form">
<p>English word/sentence:</p> <input type="text" id="english" required="required" size="80" /> <br />
<input type="button" value="Translate!" onClick="translation()" />
<p>Pig Latin translation:</p> <textarea name="piglat" rows="10" cols="60"></textarea>

<script type="text/javascript">
fucntion translation() {
var delimiter = " ";
input = document.form.english.value;
tokens = input.split(delimiter);
output = [];
len = tokens.length;

for (i = 1; i<len; i++){
output = output.join(delimiter);

I'd really appreciate any help I can get!


I think the two things you really need to be looking at are the substring() method and string concatentation (adding two strings together) in general. Being that all of the objects in the array returned from your call to split() are strings, simple string concatentation works fine. For example, using these two methods, you could move the first letter of a string to the end with something like this:

var myString = "apple";

var newString = mystring.substring(1) + mystring.substring(0,1);