I can not break from the while loop, Pig Latin Challenge

I can not break from the while loop, Pig Latin Challenge
0

#1

I am solving the pig Latin challenge, I am having all the answers right except the cluster of consonant, I made a while loop, if the character is a vowel, the loop should break, but it is not breaking and continuing looping?!

I am using the console.log to check the answers , i am getting the right answer for “glove”, but the loop continues and mixes the letters further! Any idea why this is happening ?


function translatePigLatin(str) {
  
  var vowels = ['a','e','i','o','u'];
  var newStr= str[0];
  var strLen = str.length;
  var firstChar = str.substr(0,1);
  var f = true;
  var i = 0;
  
  if (vowels.indexOf(firstChar) > -1 ){
  
    str = str + 'way';
  }
  
  else {
    
    str = str.substr(1, str.length);
    //console.log('this is the String: ' + str);
    
    
    while (i < str.length){
      if (vowels.indexOf(str[i]) == -1){
        
       newStr  += str[i];   
       str = str.substr(1, str.length);
        i++;
        console.log("If consonant: " + str);
      } 
      else if (vowels.indexOf(str[i]) > -1){ 
        break;

      }

      console.log('Whole Str: ' + str + newStr + 'ay');
      
     }
    str = str + newStr + 'ay';
  }
  
  return str;
}

translatePigLatin("glove");


#2

that part confirms the str + ''way"
and you are also trying to stop the entire loop with “else if” statement
"else" is the last logic …“else if” is just part of the looping step until you have no other option
then you run the else.

this looks like c#


#3
if (vowels.indexOf(str[i] == -1)

is used to check for a consonant,

“and you are also trying to stop the entire loop with “else if” statement
"else” is the last logic …“else if” is just part of the looping step until you have no other option
then you run the else."

I tried it with else {} , i got the same answer, it is not breaking


#4

function translatePigLatin(str)
{
var vowels = [‘a’,‘e’,‘i’,‘o’,‘u’];
var newStr= str[0];
var strLen = str.length;
var firstChar = str.substr(0,1);
var f = true;
var i = 0;

if (vowels.indexOf(firstChar) > -1 ){

str = str + 'way';

}

else {
str = str.substr(1, str.length);
//console.log('this is the String: ’ + str);

while (i <= newStr.length){
  if (vowels.indexOf(str[i]) == -1){
    
   newStr  += str[i];   
   str = str.substr(1, str.length);
    i++;
    console.log("If consonant: " + str);
  } 
  else if (vowels.indexOf(str[i]) > -1){ 
    break;

  }

  console.log('Whole Str: ' + str + newStr + 'ay');
        
 }
str = str + newStr + 'ay';

}

return str;
}

translatePigLatin(“glove”);

//This code should work, it’s fine, the loop should stop at line four and the string value will be printed at //line five — if it don’t work, your while statement is true and it will just continue…
//stop it by changing the “else if (vowels.indexOf(str[i]) > -1)” to something that stops the loop.
//instead of using else if…just use if…because else if checks to see if the while loop has stop on it’s own.
//then it calls the break, but we just want to call the break


#5

i replaced the while loop with a for loop and changed the place of the break.

Now the code works fine.

Thank you


function translatePigLatin(str) {
  
  var vowels = ['a','e','i','o','u'];
  var newStr= str[0];
  var strLen = str.length;
  var firstChar = str.substr(0,1);
  var f = true;
  var i = 0;
  
  if (vowels.indexOf(firstChar) > -1 ){ // This is to check if the first character is a vowel
  
    str = str + 'way';
  }
  
  else { // The rest will be a consonant
    
    str = str.substr(1, str.length); // return the whole word without the 1st char
     
   for( i ; i < str.length;i++){
     
      var y = vowels.indexOf(str[i]);  
        if (y>-1) { break; }
       newStr  += str[i];   
       str = str.substr(1, str.length);
        i++;
       
      } 
     
      str = str + newStr + 'ay';
     }
    

  return str;
  }
  
  
translatePigLatin("glove");