www.webdeveloper.com
Results 1 to 12 of 12

Thread: piglatin converter

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    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>

  2. #2
    Join Date
    May 2010
    Posts
    44
    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.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb

    Quote Originally Posted by Gullanian View Post
    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.
    Then compare your solution to these

  4. #4
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    "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");
    Last edited by NicTlt; 05-06-2010 at 06:27 PM.

  5. #5
    Join Date
    Apr 2010
    Posts
    5
    whoo! that's a lot of stuff i don't know about! Thanks!

  6. #6
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    And I was probably still fiddling with it as you read it...

    Here is the skinny on string functions:
    https://developer.mozilla.org/en/Cor...Objects/String

  7. #7
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    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");

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443
    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>
    Enter 'end' to stop

  9. #9
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    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 05:29 AM. Reason: ****ed up

  10. #10
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    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";
    }
    So many ways to skin that cat once you open javascript's knife drawer...

  11. #11
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    What about a translation from/to Dog Latin?

  12. #12
    Join Date
    Apr 2010
    Location
    UK
    Posts
    117
    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";
    }
    And who says javascript isn't a "proper" language?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles