/    Sign up×
Community /Pin to ProfileBookmark

piglatin converter

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>[/CODE]

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@GullanianMay 06.2010 — 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.
Copy linkTweet thisAlerts:
@JMRKERMay 06.2010 — 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.[/QUOTE]

Then compare your solution to these
Copy linkTweet thisAlerts:
@NicTltMay 06.2010 — "in" doesn't really work like that in javascript.

You could try something like:
[CODE]
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");

[/CODE]
Copy linkTweet thisAlerts:
@Codercoder123authorMay 06.2010 — whoo! that's a lot of stuff i don't know about! Thanks!
Copy linkTweet thisAlerts:
@NicTltMay 06.2010 — And I was probably still fiddling with it as you read it...:p

Here is the skinny on string functions:

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String
Copy linkTweet thisAlerts:
@NicTltMay 06.2010 — 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");
[/CODE]
Copy linkTweet thisAlerts:
@JMRKERMay 07.2010 — Slightly easier to test if you don't have to reload every time...
<i>
</i>&lt;script type="text/javascript"&gt;
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');
&lt;/script&gt;

Enter 'end' to stop
Copy linkTweet thisAlerts:
@NicTltMay 07.2010 — 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";
}
[/CODE]

Then you can do:
[CODE]
var someString = "String";
alert(someString.emPiggen());
alert("Krakatoa".emPiggen());
[/CODE]

etc.
Copy linkTweet thisAlerts:
@NicTltMay 07.2010 — Or you could even replace the for loop altogether with a regexp:
[CODE]
String.prototype.emPiggen = function() {
var i = this.indexOf(this.match(/[aeiouy]/i));
return (i < 0) ? this : this.slice(i) + this.substr(0, i) + "ae";
}

[/CODE]

So many ways to skin that cat once you open javascript's knife drawer...:eek:
Copy linkTweet thisAlerts:
@KorMay 07.2010 — What about a translation from/to Dog Latin? ?
Copy linkTweet thisAlerts:
@NicTltMay 07.2010 — And if you want to really annoy anyone tasked with maintaining your code, you can fold it down into one line:?
[CODE]
String.prototype.emPiggen = function(i) {
return ((i = this.indexOf(this.match(/[aeiouy]/i))) < 0) ? this : this.slice(i) + this.substr(0, i) + "ae";
}[/CODE]

And who says javascript isn't a "proper" language??
×

Success!

Help @Codercoder123 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...