Pig Latin question

Tell us what’s happening:
So I came up with a solution albeit complicated and it uses a few too many variables. However, at the end I am having trouble concatenating addCons to the string, the console saying that the string remains unchanged.

Your code so far


function translatePigLatin(str) {
  var arr = [];
  arr = str.split("");
  var addVow = "way";
  var addCons = "ay";
  var first = arr[0];
  var vowels =["a", "e", "i", "o", "u"];
  var counter = 0;
  var merged = str;
  
  console.log("first is "+first);
  for (let i=0; i<vowels.length; i++) {
    if (first == vowels[i]) {
      counter++;
      console.log("first is a vowel ,"+first);
    }
 }
 if (counter!==0) {
  
  merged.concat(addVow);
 } else {
   let arrSplit = merged.split("");
   arrSplit[0]="";
   console.log("merged is "+merged)
   console.log("arrSplit is "+arrSplit);
   merged = arrSplit.join("");
   console.log("the new merged is "+merged);
   merged.concat(addCons);
   ("the final merged is"+merged);
 }
 return merged;
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/

From the docs, This method does not change the existing arrays, but instead returns a new array.

I always have to double check with these prototype methods - some of them change the original and some of them leave the original unchanged and simply return the new value - this one does the latter.

Your line:

merged.concat(addCons);

does the merge, leaving merged unchanged and returns the merged string, but because it isn’t assigned to anything, it is lost in the ether. You want something like:

merged = merged.concat(addCons);

That way it gets saved back into itself.

Also, without a console.log, the line that follows that is doing nothing.

Let us know if you need more help.

I fixed what you have pointed out and also noticed that the algorithm needs to work on the first consonant cluster, not just the first consonant. I tried to do that with a while loop which ended disastrously haha. Please take a look:

function translatePigLatin(str) {
  var arr = [];
  arr = str.split("");
  var addVow = "way";
  var addCons = "ay";
  var first = arr[0];
  var vowels =["a", "e", "i", "o", "u"];
  var counter = 0;
  var merged = str;
  
  console.log("first is "+first);
  for (let i=0; i<vowels.length; i++) {
    if (first == vowels[i]) {
      counter++;
      console.log("first is a vowel ,"+first);
    }
 }
 if (counter!==0) {
  
  merged = merged.concat(addVow);
 } else {
   let arrSplit = merged.split("");
   while (arrSplit[0]!==vowels[0] && arrSplit[0]!==vowels[1] && arrSplit!==vowels[2] && arrSplit[0]!==vowels[3] && arrSplit[0]!==vowels[4]) {
   arrSplit.splice(arrSplit.length,0,arrSplit[0]);
   arrSplit[0]="";
   }
   console.log("merged is "+merged)
   console.log("arrSplit is "+arrSplit);
   merged = arrSplit.join("");
   console.log("the new merged is "+merged);
   merged = merged.concat(addCons);
   console.log("the final merged is "+merged);
 }
 return merged;
}

translatePigLatin("consonant");

@andy_bruja

while (arrSplit[0]!==vowels[0] && arrSplit[0]!==vowels[1] && arrSplit!==vowels[2] && arrSplit[0]!==vowels[3] && arrSplit[0]!==vowels[4]) {
  arrSplit.splice(arrSplit.length,0,arrSplit[0]);
  arrSplit[0]="";
  }

I think this code means: While the first item in the array doesn’t equal a vowel, add that item to the end of the array, and set the first item of the array to an empty string. Then, start over with the first item in the array, which is now an empty string. This will go on forever because an empty string will never equal a vowel.

I think you’ll need to remove the first item in the array instead of setting it to an empty string.

Okay, I gave it some more thought for a while and came up with this. I also commented out a part. Not sure if it would be more useful than what I left as the main code. The only thing is that I don’t know how to solve the last challenge? What do they actually mean by “handle”.

function translatePigLatin(str) {
  var arr = [];
  arr = str.split("");
  var addVow = "way";
  var addCons = "ay";
  var first = arr[0];
  var vowels =["a", "e", "i", "o", "u"];
  var counter = 0;
 // var counter2 = 0;
 // var counter3 = 0;
  var counterM = 0;
  var merged = str;
  
  console.log("first is "+first);
  for (let i=0; i<vowels.length; i++) {
    if (first == vowels[i]) {
      counter++;
      console.log("first is a vowel ,"+first);
    }
 }
 if (counter!==0) {
  
  merged = merged.concat(addVow);
 } else {
   let arrSplit = merged.split("");

function getCounter(split) {
  for (let m=0; m<split.length;m++) {
counterM++;
if (arrSplit[m]==vowels[0] || arrSplit[m]==vowels[1] || arrSplit[m]==vowels[2] || arrSplit[m]==vowels[3] || arrSplit[m]==vowels[4]) {
  return counterM-1;
    }
  }
}
console.log("counterM for arrSplit is "+ getCounter(arrSplit));
for (let k=0;k<counterM-1;k++) {
   arrSplit.splice(arrSplit.length,0,arrSplit[k]);
}

  arrSplit.splice(0,counterM-1);


  /* for (let j=0;j<arrSplit.length;j++) {
     while (arrSplit[j]!==vowels[0] && arrSplit[j]!==vowels[1] && arrSplit[j]!==vowels[2] && arrSplit[j]!==vowels[3] && arrSplit[j]!==vowels[4] && counter2==0) {
       arrSplit.splice(arrSplit.length,0,arrSplit[j]);
       counter2 ++;
       counter3 ++;
       console.log("in the while loop arrSplit is "+ arrSplit);
     }
counter2 = 0;
for (let x=0; x<counter3; x++){
  arrSplit[x]="";
}
   }*/


   console.log("merged is "+merged);
   console.log("arrSplit is "+arrSplit);
   merged = arrSplit.join("");
   console.log("the new merged is "+merged);
   merged = merged.concat(addCons);
   console.log("the final merged is "+merged);
 }
 return merged;
}

translatePigLatin("consonant");
if (arrSplit[m]==vowels[0] || arrSplit[m]==vowels[1] || arrSplit[m]==vowels[2] || arrSplit[m]==vowels[3] || arrSplit[m]==vowels[4])

I don’t have time right now to go through all the code, but I did notice that this could be simplified to this:

if (vowels.includes(arrSplit[m])) 
1 Like

I noticed that counterM is undefined when the word is only made of consonants. I added an if statement checking for this on line 36, but it doesn’t seem to do much. It didn’t work with typeof either:

function translatePigLatin(str) {
  var arr = [];
  arr = str.split("");
  var addVow = "way";
  var addCons = "ay";
  var first = arr[0];
  var vowels =["a", "e", "i", "o", "u"];
  var counter = 0;
 // var counter2 = 0;
 // var counter3 = 0;
  var counterM = 0;
  var merged = str;
  
  console.log("first is "+first);
  for (let i=0; i<vowels.length; i++) {
    if (first == vowels[i]) {
      counter++;
      console.log("first is a vowel ,"+first);
    }
 }
 if (counter!==0) {
  merged = merged.concat(addVow);
  
 } else {
   let arrSplit = merged.split("");

function getCounter(split) {
  for (let m=0; m<split.length;m++) {
counterM++;
if (arrSplit[m]==vowels[0] || arrSplit[m]==vowels[1] || arrSplit[m]==vowels[2] || arrSplit[m]==vowels[3] || arrSplit[m]==vowels[4]) {
  return counterM-1;
}
  }
}
console.log("counterM for arrSplit is "+ getCounter(arrSplit));
   if (counterM === undefined) {
   merged = merged.concat(addVow);
   console.log("merged when there are consonants only is "+merged);
   return merged;
 }
for (let k=0;k<counterM-1;k++) {
   arrSplit.splice(arrSplit.length,0,arrSplit[k]);
}

  arrSplit.splice(0,counterM-1);


  /* for (let j=0;j<arrSplit.length;j++) {
     while (arrSplit[j]!==vowels[0] && arrSplit[j]!==vowels[1] && arrSplit[j]!==vowels[2] && arrSplit[j]!==vowels[3] && arrSplit[j]!==vowels[4] && counter2==0) {
       arrSplit.splice(arrSplit.length,0,arrSplit[j]);
       counter2 ++;
       counter3 ++;
       console.log("in the while loop arrSplit is "+ arrSplit);
     }
counter2 = 0;
for (let x=0; x<counter3; x++){
  arrSplit[x]="";
}
   }*/



   console.log("merged is "+merged);
   console.log("arrSplit is "+arrSplit);
   merged = arrSplit.join("");
   console.log("the new merged is "+merged);
   merged = merged.concat(addCons);
   console.log("the final merged is "+merged);
 }
 
 return merged;
}

translatePigLatin("rhythm");

Why is it undefined?

Let’s see why on a word like “rhythm” getCounter(arrSplit) would return undefined. I properly indented your function code above to make it easier to read.

Since arrSplit would be [ ‘r’, ‘h’, ‘y’, ‘t’, ‘h’, ‘m’ ] in this test case, your function would loop through arrSplit and when a vowel is found, it would return counterM - 1. However, since there are no vowels in arrSplit, your return statement never executes and the function exits after the for loop. What you may not know is if a function does not explicitly return a value, JavaScript will return the value undefined by default. This is what happens in this case.

Thank you. I finally solved it.

function translatePigLatin(str) {
  var arr = [];
  arr = str.split("");
  var addVow = "way";
  var addCons = "ay";
  var first = arr[0];
  var vowels =["a", "e", "i", "o", "u"];
  var counter = 0;
  var a = 0;
 // var counter2 = 0;
 // var counter3 = 0;
  var counterM = 0;
  var merged = str;
  
  console.log("first is "+first);
  for (let i=0; i<vowels.length; i++) {
    if (first == vowels[i]) {
      counter++;
      console.log("first is a vowel , " + first);
    }
 }
 if (counter!==0) {
  merged = merged.concat(addVow);
  
 } else {
   let arrSplit = merged.split("");

function getCounter(split) {
  for (let m=0; m<split.length;m++) {
counterM++;
if (arrSplit[m]==vowels[0] || arrSplit[m]==vowels[1] || arrSplit[m]==vowels[2] || arrSplit[m]==vowels[3] || arrSplit[m]==vowels[4]) {
   a = counterM-1
  return a;
    }
    
  } return a;
}
console.log("counterM for arrSplit is "+ getCounter(arrSplit));
console.log("that means a is " + a);
   if (a == 0) {
   merged = merged.concat(addCons);
   console.log("merged when there are consonants only is "+merged);
   return merged;
 }
for (let k=0;k<counterM-1;k++) {
   arrSplit.splice(arrSplit.length,0,arrSplit[k]);
}

  arrSplit.splice(0,counterM-1);


  /* for (let j=0;j<arrSplit.length;j++) {
     while (arrSplit[j]!==vowels[0] && arrSplit[j]!==vowels[1] && arrSplit[j]!==vowels[2] && arrSplit[j]!==vowels[3] && arrSplit[j]!==vowels[4] && counter2==0) {
       arrSplit.splice(arrSplit.length,0,arrSplit[j]);
       counter2 ++;
       counter3 ++;
       console.log("in the while loop arrSplit is "+ arrSplit);
     }
counter2 = 0;
for (let x=0; x<counter3; x++){
  arrSplit[x]="";
}
   }*/



   console.log("merged is "+merged);
   console.log("arrSplit is "+arrSplit);
   merged = arrSplit.join("");
   console.log("the new merged is "+merged);
   merged = merged.concat(addCons);
   console.log("the final merged is "+merged);
 }
 
 return merged;
}

translatePigLatin("rhythm");