HI! I'm making a program that lets you type in a word and converts it to pig latin. I wrote it out in Python originally, and the program doesn't work. Is there something wrong with my for loop??
Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE = "JavaScript">
var original = prompt('Type a word');
var count = 0
var istrue = False
var suffix = ""
var modified = ""
var letter;
for (letter in original)
{
if (!(istrue)){
if(letter (!(in "aeiouy")){
suffix = suffix + letter;
}
if(letter in "aeiouy"){
istrue = True;
}
}
if(istrue){
modified = modified + letter;
}
}
alert(modified + suffix + "ae");
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Run it in FireFox and go to the error console, it's the quickest way to locate errors. From what you have written I can see a few things that would probably fail.
Run it in FireFox and go to the error console, it's the quickest way to locate errors. From what you have written I can see a few things that would probably fail.
var original = prompt("Type a word");
for (var i = 0, letter; letter = original[i]; i++) {
if ("aeiouy".indexOf(letter) > -1) {
break;
}
}
alert(original.slice(i) + original.substr(0, i) + "ae");
Hmm still a bit long-winded - no real need for a breaking if statement in a for loop:
Code:
var original = prompt("Type a word");
for (var i = 0; "aeiouy".indexOf(original[i]) === -1; i++);
alert(original.slice(i) + original.substr(0, i) + "ae");
Slightly easier to test if you don't have to reload every time...
Code:
<script type="text/javascript">
do {
var original = prompt("Type a word");
for (var i = 0; "aeiouy".indexOf(original[i]) === -1; i++);
alert(original.slice(i) + original.substr(0, i) + "ae");
} while (original != 'end');
</script>
Probably best to check for end of string as well, to be honest, as the last one really doesn't cope with (accidental) all-consonant words. And I'm not sure IE supports string indexing.
If you wanted to make it more generic, you could do:
Code:
String.prototype.emPiggen = function () {
for (var i = 0, letter; ((letter = this.substr(i, 1)) && ("aeiouy".indexOf(letter) < 0)); i++);
return this.slice(i) + this.substr(0, i) + "ae";
}
Then you can do:
Code:
var someString = "String";
alert(someString.emPiggen());
alert("Krakatoa".emPiggen());
etc.
Last edited by NicTlt; 05-07-2010 at 04:29 AM.
Reason: ****ed up
Bookmarks