Pig Latin - help [SOLVED]

Hi, I wrote the following code for Pig Latin, and it works for the words given as examples. But it seems there is a problem with the first 3 ones: california, paragraphs and glove. If you check them the return gives the same strings as they say in example. However I don’t know what is the problem here.
So please Help!!!

var del = [];
var newstr = "";
var arr = [];

function translatePigLatin(str) {
  if ( vowel(str[0]) === true) {
     return str +'way';
    }
    else {
  for ( var i=1; i<str.length; i++) {
        if( vowel(str[i]) === true) {
          newstr = str.substr(i);
          for( var j=0; j<i; j++){
                del[j] = str[j];
          }
          for( var x=0; x<newstr.length; x++){
           arr[x] = newstr[x];
          }
 
      Array.prototype.push.apply(arr,del);
      return arr.join("") +"ay";
         }
        }
   
    }   
}
function vowel (l){
  var vowels = ["a","e","i","o","u"];
  if (vowels.indexOf(l)!==-1)
    {
      return true;}
  else {
    return false;}
}

translatePigLatin("paragraphs");

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

You are using global variables.

Global variables persist after function calls have completed, so we have to be very careful about modifying globals in functions. Your code relies on arr and del being empty arrays and newstr being an empty string when the function is called, but after translatePigLatin has been executed, they no longer are. This means that your function not work every time.

Oh Thank you very much!! Just put them inside the function and it works. :slight_smile:

Thanks for the cleaning up. I will use triple backticks next time I post code here.