SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    for loop problem?

    Hello,

    I'm trying to complete an exercise question I found online:

    "Write a function translate() that will translate a text into "rövarspråket". That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string 'tothohisos isos fofunon'."

    I think I'm nearly there with it but I have an issue with a for loop I wondered if someone might be able to help me out with. Below is my code:

    Code HTML4Strict:
    <body>
     
    <p id="p1">this is my string</p>
    <p id="p2"></p>
     
    <script language="JavaScript" type="text/javascript">
     
    var myString = document.getElementById("p1").innerHTML;
    vowels = new Array("a","e","i","o","u");
     
    for (var i = 0, j = myString.length; i < j; i++) {
    	var letter = myString.substring(i, i + 1);
    	for (var k = 0, m = vowels.length; k < m; k++) {
    	    var vowel = vowels[k];
    			if (letter != vowel) {
    				letter = letter + "o" + letter;
    			}
    	}	
    	document.getElementById("p2").innerHTML += letter;
    }
     
    </script>
     
    </body>

    When I run this code I get loads all of the characters in the string repeated loads of time with the 'o' in the middle? I'm thinking it might be to do with me altering the letter variable in the second for loop and the loop then using that letter value to run again?

    What I really don't get is, if I change the code so it does the opposite to what I want (add those extra characters if the letter is equal to a vowel) it works great? e.g.

    Code JavaScript:
    if (letter == vowel) {
    letter = letter + "o" + letter;
    }

    Please let me know if you need me to explain more?

    Thanks

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The reason you're getting loads of "o"s is because each consonant is going to match the (letter != vowel) every time of the loop. So for , say, "t", you will get "tot" five times. You need to do the opposite, which you seem to have figured out, but save the result:

    Code:
    var isvowel = false, vowel; // declare vowel before the loop. Good practice.
    for (var k = 0, m = vowels.length; k < m; k++) {
      vowel = vowels[k]; 
      if (letter == vowel) {
        isvowel = true;
        break;
       }
    }
    if (!isvowel) // it's a consonant, do the repetition.
    I would also operate on a completely new string and then finally using innerHTML. It seems neater and it means you're not using innerHTML lots of times.

    You may want to consider using split() instead of substring - it just seems like a more logical option to separate the string into an array (into chunks) rather than extracting a letter from the original string each time.

    Finally, as another exercise, you could replace the nested for loop with a simple regular expression match:

    Code:
    if (/[aeiou]/).test(letter)) {
      // it's a vowel
    }

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks Raffles, that makes more sense to me. I'll give what you have suggested a try and let you know how I get on.

  4. #4
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked like a charm! Thanks again for all your help and advice Raffles.


Bookmarks

Posting Permissions

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