How infinite loop?

How infinite loop?
0

#1
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.)


#2

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.


#3

@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.


#4

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.

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’?


#5
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. @RandellDawson, thanks for the specific instructions on formatting.


#6

FYI - You do not actually need to create the letters array or the ble array, but you can use similar logic/syntax to solve it.

function translatePigLatin(str) {
  vowel = ['a','e','i','o','u'];
  
  //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;
  }
   x = 0;
  var newStr = "";
  while (!is_vowel(str[x])){
    newStr += str[x++];
  }
  if (newStr.length){
    new_letters = str.slice(newStr.length);
    newStr = new_letters + newStr + 'ay';
  }
  else{
    newStr = str + 'way';
  }
  return newStr;
}

Also, you could simplify your is_vowel function several ways and make it more efficient (stop looping once vowel is found) by.

  function is_vowel(lett){
    for (i = 0; i < vowel.length; i++){
      if (vowel[i] === lett){
        return true;
      }
    }
    return false;
  }

OR using indexOf function

  function is_vowel(lett){
    return vowel.indexOf(lett) >= 0;
  }

OR using includes functtion

  function is_vowel(lett){
    return vowel.includes(letter);
  }

OR forget the vowel array and use a regular expression

  function is_vowel(lett){
    return /[aeiou]/.test(lett);
  }

#7

Thanks again for these great suggestions, @RandellDawson. 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!