Regex test to see the content of a array element

  var string_array=[];
 var con =new RegExp(/[bcdfghjklmnpqrstvwxyz.]/);
 string_array = str.split('');

   for(var i=0;i<string_array;i++){
      if(con.test(string_array[i])){
      string_array.shift(string_array[i]);
      console.log(shifted_array);
     }

what would be wrong would this somehow the test method on the array element doesn’t seem to work,even though the array element is a string.
Could someone please help?

You’re using console.log(shifted_array) but shifted_array is never declared or defined before then. That’s at least one part of the problem.

I also do not see where you declare str or assign it a value, but you try to split it on the 3rd line above.

Sorry this is the whole code.I was just trying to concentrate on that small part of it as I don’t think there is anything wrong
with the rest.


function translatePigLatin(str) {
  var bunch_of_constanents =[];
  var string_array=[];
  var vowel = new RegExp(/[aioue.]/);
  var con =new RegExp(/[bcdfghjklmnpqrstvwxyz.]/);
  var suffix = "way";
  var shifted_array =[];
  var how_Long = str.length;
  
  if(vowel.test(str.charAt(0))){
    var str2 = str+suffix;
    return str2;
    
  }
 
  string_array = str.split('');
  
   for(var i=0;i<string_array;i++){
      if(con.test(string_array[i])){
      string_array.shift(string_array[i]);
      console.log(shifted_array);
     }
     
      else{
           break;
     }
      
   } 
 

}
 
translatePigLatin("california");

This is the whole code.I was just trying to concentrate on a small area of it in the previous post.


function translatePigLatin(str) {
  var bunch_of_constanents =[];
  var string_array=[];
  var vowel = new RegExp(/[aioue.]/);
  var con =new RegExp(/[bcdfghjklmnpqrstvwxyz.]/);
  var suffix = "way";
  var shifted_array =[];
  var how_Long = str.length;
  
  if(vowel.test(str.charAt(0))){
    var str2 = str+suffix;
    return str2;
    
  }
 
  string_array = str.split('');
  
   for(var i=0;i<string_array;i++){
      if(con.test(string_array[i])){
      string_array.shift(string_array[i]);
      console.log(shifted_array);
     }
     
      else{
           break;
     }
      
   } 
 

}
 
translatePigLatin("california");

Do you happen to see a problem with the following line? Do you think any part of the code within your for loop runs?

for(var i=0;i<string_array;i++){

Also, you have the following line, but I do not see anywhere in your code where you change the contents of shifted_array after your first initialize it on line 7 with a blank array.

console.log(shifted_array);

I see what you mean.

for(var i=0;i<string_array.length;i++)

I missed out adding the length method.
And then

I was gonna do this with it.

for(var i=0;i<string_array;i++){
      if(con.test(string_array[i])){
      shifted_array  =string_array.shift(string_array[i]);
     string_array.push(shifted_array);
      console.log(shifted_array);
     }

basically using shift and push method to take it from the front of the array and put it on the back.I will try it out later thanks for your help.

Any regex with /[{a load of other characters}.]/ will match any character other than line breaks, because that’s what the dot . signifies. So vowel and con are functionally the same as each other.

Right managed to do it after a good few hours but its a bit long what do you think?
What gave me a problem was that the shift method was changing the order of the array.So I simply changed this because the shift function kinda iterates the array for you.

for(var i=0;i<string_array.length;i++)

to this

for(var i=0;i<string_array.length;)

function translatePigLatin(str) {
  
  var string_array=[];
  var vowel = /[aioue]/;
  var con = /[bcdfghjklmnpqrstvwxyz]/;
  var suffix = "way";
  var consonant_suffix ="ay";
  var shifted_array =[];
  var consonant=" ";
  
  
  if(vowel.test(str.charAt(0))){
    var str2 = str+suffix;
    return str2;
    
   
  }
  
   string_array = str.split('');
  
  
   for(var i=0;i<string_array.length;){
     if(con.test(string_array[i])){
      console.log(consonant);
      consonant = string_array.shift();
      shifted_array.push(consonant);
       
      }
     
     else{
      var new_string_array = string_array.join('');
      var new_shifted_array = shifted_array.join('');
      var str3 = new_string_array.concat(new_shifted_array+consonant_suffix);
      return str3;
     }
   }
  
     
      
}
 
translatePigLatin("glove");

Thanks changed it now been a long time since I did a regex pattern.

You don’t really need a con regular expression, because you could just write:

if(!vowel.test(string_array[i])){ // tests if not a vowel