How infinite loop?

function translatePigLatin(str) {
  vowel = ['a','e','i','o','u'];
  //split the string
  letters = str.split('');
  
  //check to see if a letter is a vowel i.e. contained in 'vowel' array
  function is_vowel(lett){
    yep_vowel = false;
    for (i = 0; i < vowel.length; i++){
      if (vowel[i] === lett){
        yep_vowel = true;
      }
    }
    return yep_vowel;
  }
  //cons = grab everything up to the first vowel
      //cons_mark = return marker that was used to grab cons
  //splice from beginning to cons_mark
  i = 0;
  ble = [];
  while (!is_vowel(letters[i])){
    ble.push(letters[i]);
    i ++;
  }
  //push cons to pig_str
  //push add 'ay' to end of string or array
  //join everything together
  return str;
}

translatePigLatin("consonant");

Above is my code for the pig-latin intermediate algorithm scripting. If i replace “ble.push(letters[i])” with “return letters[i]” then there is no loop problem (infinte loop–at least, that’s what i assume). Why is there a loop problem with ble.push(letters[i])? Also, the page keeps crashing. I never disabled loop protection. Why has it ceased to protect me? (I found a post on manually enabling it via url encoding, so no worries there.)

Fixed this.

I think the fact that i used “i” as my index variable both in the “is_vowel” function and in the part where i saw the error was a problem.

@bdfoz,

I don’t know if the code you posted is your original code, but naming several variables the same name was the least of your problems.

function translatePigLatin(str) {
         vowel = [‘a’,‘e’,‘i’,‘o’,‘u’];
         //split the string
         letters = str.split(’’);

        //check to see if a letter is a vowel i.e. contained in ‘vowel’ array
       function is_vowel(lett){
                yep_vowel = false;
               for (i = 0; i < vowel.length; i++){
                         if (vowel[i] === lett){
                                yep_vowel = true;
                             }
               }
     return yep_vowel;
}

I aligned your code for readability.
What you have in your code above is a declaration of a function inside of another function. This cannot happen. This was surely giving you problems.

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

There is nothing wrong with declaring a function inside another function.

To help you figure out what is wrong, you need to think about what value you want your translatePigLatin function to return. Right now, you are returning str, which is the original string and has not been converted to Pig Latin. Because you declared i as a global variable with i=0; your i in your for loop can cause trouble in your main code. You need to declare the i in your for loop, so that it’s scope is only inside the is_vowel function. Then, you need to look at the logic of your while loop condition, because once you fix the scoping issue with the variable i, the loop will stop once it tries to enter the 2nd iteration of the while loop when letters[1] or ‘o’ is encountered. At this point in the code with the example test of ‘consonant’, your ble array will look like [‘c’]. What do you plan on doing with the ble array to help you convert ‘consonant’ into ‘onsonantcay’?

function translatePigLatin(str) {
  vowel = ['a','e','i','o','u'];
  //split the string
  letters = str.split('');
  
  //check to see if a letter is a vowel i.e. contained in 'vowel' array
  function is_vowel(lett){
    yep_vowel = false;
    for (i = 0; i < vowel.length; i++){
      if (vowel[i] === lett){
        yep_vowel = true;
      }
    }
    return yep_vowel;
  }
  //cons = grab everything up to the first vowel
      //cons_mark = return marker that was used to grab cons
  //splice from beginning to cons_mark
  x = 0;
  ble = [];
  while (!is_vowel(letters[x])){
    ble.push(letters[x]);
    x ++;
  }
  if (ble.length > 0){
    new_letters = letters.splice(ble.length);
    ble.push('a','y');
    str = new_letters.join('') + ble.join('');
  }
  else{
    str = str + 'way';
  }
  //push cons to pig_str
  //push add 'ay' to end of string or array
  //join everything together
  return str;
}

translatePigLatin("algorithm");

Thanks for the help, all. Here’s what I ended up with that worked. @camperextraordinaire, thanks for the specific instructions on formatting.

Thanks again for these great suggestions, @camperextraordinaire. I guess i lack enough familiarity with the functions you used to know to go to them. Comes with time, and great instruction. Thanks for some of the latter!