I have to convert english to pig latin for an assignment. I am having trouble with my code.
The assignment is that I have 2 separated functions on a page – one that does an individual word only and the other that will convert a whole phrase.
What am I doing wrong here? I am getting error messages but I cannot figure them out. I guess I have banged my head against the wall too many times to count over this code.
Here is my code so far:
<html>
<head>
<script type=”text/javascript” src=”convert.js”>
</script>
</head>
<body>
<body style=”background-color:silver”>
<b>Enter the word that you want to convert to Pig Latin:</b> <input id=”src” type=”text” name=”src”>
<p>
<input type=”button” value=”Translation” onClick=”Piglatin();>
<p>
<p>
<b>Gets you this in Pig Latin:</b>
<input id=”src” type=”text” name=”src”>
<p>
<p>
<p>
<hr />
<b>Enter phrase you want to translate into Pig Latin:</b> <input id=”src1″ type=”text” name=”src”>
<p>
<p>
<p>
<input type=”button” value=”Translation” onClick=”Piglat2();”>
<p>
<p>
<b>The phrase in Pig Latin is:</b> <input id=”src1″ type=”text” name=”dest1″>
<p>
<input value=”Clear Text” onclick=”ClearText()” type=”button”>
<p>
</body>
</html>
The convert.js is this:
function isLetter(ch) {
var ret = false;
if ((ch == ‘a’) || (ch == ‘A’) ||
(ch == ‘e’) || (ch == ‘E’) ||
(ch == ‘i’) || (ch == ‘I’) ||
(ch == ‘o’) || (ch == ‘O’) ||
(ch == ‘u’) || (ch == ‘U’)) {
ret = true;
}
{
return(ret);
}
function Piglatin()
{
var dest = “”;
var src = document.getElementById(“src”).value;
if (isVowel(src.charAt(0))) {
dest = src + “way”;
} else {
var startLet = “”;
while (!isVowel(src.charAt(0)) && src.length > 0)
{
startLet = startLet + src.charAt(0);
src = src.substring(1,src.length);
}
dest = src + startLet + “way”;
}
{
document.getElementById(“dest”).value = dest;
}
function Piglat2() {
var dest1 = “”;
var src1 = document.getElementById(“src1”).value;
if (isLetter(src1.charAt(0))) {
dest1 = src1 + “way”;
} else {
var startLet = “”;
while (!isLetter(src1.charAt(0)) && src.length > 0) {
startLet = startLet + src1.charAt(0);
src1 = src1.substring(1,src.length);
}
dest1 = src + startLet + “way”;
}
<i>
</i>function isLetter(ch) {
ch = ch.toLowerCase();
var ret = false;
if ( (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') ) {
ret = true;
}
return ret;
}
<!DOCTYPE HTML>
<html>
<head>
<title>Pig Latin Translator</title>
</head>
<body style="background-color:silver">
<form action='#'>
<b>Enter phrase you want to translate to Pig Latin:</b><br>
<input type="text" name="userInput" size='60'>
<p>
<p>
<p>
<input type="button" value="Translation" onClick="dest1.value=Piglat( userInput.value );">
<p>
<p>
<b>The phrase in Pig Latin is:</b><br>
<input type="text" name="dest1" size='60'>
<p>
<input value="Clear Text" onclick="userInput.value=dest1.value=''" type="button">
</form>
<script type="text/javascript">
function Piglat( str )
{
var plStr = "",
re=/(s*)([aeiou]*)([b-df-hj-np-tv-z]*)([a-z]+)/ig,
result;
while( result = re.exec( str ) )
plStr += result[ 1 ] + result[ 2 ] + result[ 4 ] + result [ 3 ] + 'ay';
return plStr; <br/>
}
</script>
</body>
</html>
[/SIZE][/QUOTE]
<HTML>
<HEAD>
<SCRIPT TYPE="TEXT/JAVASCRIPT" src="convert.js">
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="#" NAME="conv">
Enter what you wish to convert to Pig Latin:<input type="text" id="english" size="5" value="" />
<input type="button" name="translate" value="Translate" onclick="
getElementById('answerbox').innerHTML= convert(
document.getElementById('english').value
);"
/>
<!-- <input type="button" name="clear" value="Clear" onclick="document.getElementById('answerbox').innerHTML = '';" -->
<BR>
This is what it is in Pig Latin:<INPUT TYPE=TEXT NAME="piglatin">
</FORM>
<hr />
</BODY>
</HTML>
[/SIZE]function convert() { [/QUOTE]
var word = document.conv.english.value.split (" ")
output = ""
for (i = 0; i < word.length; i++) {
thisword = word [i]
output = output + thisword.substring (1, thisword.length) + thisword.substring(0,1) + "way "
}
document.conv.piglatin.value = output
}
[CODE]function isLetter(ch) {
ch = ch.toUpperCase();
return ( (ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U') ) || false;
}[/CODE]
and I'm sure some smarty will have an even shorter RegExp version[CODE]{
document.getElementById("dest").value = dest;
}[/CODE]
which wouldn't work if the script is loaded in the head of the HTML because the script is parsed and the interpreter sees that this is part of the main body script, executes it and its a gamble that the object exists at the time of JS making the call.[CODE]function init(){
// do stuff like set up the object Refs.
dataSrc1 = document.getElementsById("src1") || false;
dataSrc2 = document.getElementsById("src2") || false;
//... etc,
}
window.onload = init;[/CODE]
[code=html]<input value="Clear Text" onclick="ClearText()" type="button">[/code]
would be better as a plain old "Reset" button which would do the same thing as the ClearText() function.orYou can shorten it without resorting to regex. (It should be called isVowel)
and I'm sure some smarty will have an even shorter RegExp version[/quote]
function isLetter(ch) {
ch = ch.toUpperCase();
return ( (ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U') ) || false;
}
function isVowel( ch )
{
return 'aeiou'.indexOf( ch.toLowerCase() ) > -1 ;
}
function convert() {
var word = document.conv.english.value.split (" ")
output = ""
for (i = 0; i < word.length; i++) {
thisword = word [i]
output = output + thisword.substring (1, thisword.length) + thisword.substring(0,1) + "way "
}
document.conv.piglatin.value = output
}
[/quote]
As I understand it, Pig Latin involves transferring the first group of consonants to the end of the word then appending 'ay' or similar....
Any suggestions as to how to search for all the extra {} that I have in there? Earlier I was told that I had unneeded ones and cannot see them for myself. I have used notepad++ to try to show the elements that it does but it is Greek to me.
...[/QUOTE]
<i>
</i>function isLetter(ch) {
var ret = false;
if (
(ch == 'a') || (ch == 'A') ||
(ch == 'e') || (ch == 'E') ||
(ch == 'i') || (ch == 'I') ||
(ch == 'o') || (ch == 'O') ||
(ch == 'u') || (ch == 'U')
) { ret = true; }
[COLOR="Red"]{[/COLOR]
return[COLOR="Red"]([/COLOR]ret[COLOR="Red"])[/COLOR];
}
function Piglatin() {
var dest = "";
var src = document.getElementById("src").value;
if (isVowel(src.charAt(0))) {
dest = src + "way";
} else {
var startLet = "";
while (!isVowel(src.charAt(0)) && src.length > 0) {
startLet = startLet + src.charAt(0);
src = src.substring(1,src.length);
}
dest = src + startLet + "way";
}
[COLOR="Red"]{
[/COLOR] document.getElementById("dest").value = dest;
}
function Piglat2() {
var dest1 = "";
var src1 = document.getElementById("src1").value;
if (isLetter(src1.charAt(0))) {
dest1 = src1 + "way";
} else {
var startLet = "";
while (!isLetter(src1.charAt(0)) && src.length > 0) {
startLet = startLet + src1.charAt(0);
src1 = src1.substring(1,src.length);
}
dest1 = src + startLet + "way";
}
[COLOR="Lime"]}
[/COLOR]
index = word.search(/[aeiouAEIOU]/);
function PigLatin(word)
{
var index;
<i> </i> index = word.search(/[aeiouAEIOU]/); /* Simpler to use the i flag: /[aeiou]/i */
respond if (index <= 0) { [COLOR=Red]/*Where is 'respond' defined? It has no effect here */[/COLOR]
word + "way"; /* this line has no effect */
}
else {
word.substring(index, word.length) + word.substring(0,index) + "ay"; [COLOR=Red] /* This line has no effect */[/COLOR]
return respond [COLOR=Red]/* 'respond' has no value assigned to it in this function */[/COLOR]
}
}
[/quote]
This function does nothing.<i>
</i><html>
<head>
<title> PigLatin Converter </title>
<script type="text/javascript">
function LatinWord(word) {
var index;
index = word.search(/[aeiouAEIOU]/);
if (index <= 0) { return word + "way"; }
else { return word.substring(index, word.length) + word.substring(0,index) + "ay"; }
}
function LatinPhrase(phrase){
var str = '';
var tarr = phrase.split(' ');
for (var i=0; i<tarr.length; ++i) { str += LatinWord(tarr[i])+' '; }
return str;
}
</script>
</head>
<body>
<form name="PigForm" onsubmit="return false">
Enter a word:<br /> <input type="text" name="word" size=40 value="" />
<p />
<input type="button" value="Translate to Pig Latin"
onClick="document.PigForm.pigWord.value=LatinWord(document.PigForm.word.value);" />
<p />
In Pig Latin:<br /> <input type="text" name="pigWord" size=40 value="" />
<p />
<hr>
<p />
Enter a phrase/sentence:<br /> <input type="text" name="phrase" size=40 value="" />
<p />
<input type="button" value="Translate to Pig Latin"
onClick="document.PigForm.pigPhrase.value=LatinPhrase(document.PigForm.phrase.value);" />
<p />
In Pig Latin:<br /> <input type="text" name="pigPhrase" size=40 value="" />
</form>
</body>
</html>
function PigLatin(word)
{
var index;
if (IsWord(word) == "phrase") {
return "Please enter a single word."
}
else {
index = word.search(/[aeiouAEIOU]/);
if (index <= 0) {
return word + "way";
}
else {
return word.substring(index, word.length) + word.substring(0,index) + ay;
}
}
PigLatin is "ay" appended and up to two consonants can be shifted .... upidstay[/QUOTE]
0.1.9 — BETA 6.2