(Cant solve )Pig Latin

Tell us what’s happening:

Anything wrong with my code?

  1. I was thinkin of split all the characters of the string to array first
  2. Then check whether the first character is a vowel, if yes, change it to w
  3. Store the first character with + ‘ay’ in to an array
  4. Remove the first character of the string in the array
  5. Join all those things together

Your code so far


function translatePigLatin(str) {

str.split("");

if(str[0] == 'a' || str[0] == 'e' || str[0] == 'i' || str[0] == 'o' || str[0] == 'u'){
  str[0] = 'w';
}

var arr = str[0]+"ay";
str.splice(0,1);


return console.log(str.concat(arr).join());
}

translatePigLatin("consonant");

Your browser information:

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

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

Well, the following line, does do that, but you need to assign the result to a variable which can be referenced later in your code.

str.split("");

Also, join takes an argument.

You have some other logic errors which you will need to sort out also.

What does it mean .join() takes an argument?

Here is my current code

function translatePigLatin(str) {

var arr = str.split("");

if(arr[0] == 'a' || arr[0] == 'e' || arr[0] == 'i' || arr[0] == 'o' || arr[0] == 'u'){
  arr[0] = 'w';
}

var arr1 = arr[0]+"ay";
arr.splice(0,1);


return console.log(arr.concat(arr1).join());
}

translatePigLatin("consonant");

It returns o,n,s,o,n,a,n,t,cay . I think i have problems on joining my arr

If you use .join() without an argument it will join using commas. You can provide your own string to use for joining (e.g. .join('+') will join using + symbol).

Hi @jingxuan98,

Refer to this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Oh i get it! Is there anyway to fix my current code then? Already edited and add argument to it. But i only able to pass some of the requirements

function translatePigLatin(str) {

var arr = str.split("");

if(arr[0] == 'a' || arr[0] == 'e' || arr[0] == 'i' || arr[0] == 'o' || arr[0] == 'u'){
  arr[0] = 'w';
}

var arr1 = arr[0]+"ay";
arr.splice(0,1);


return arr.concat(arr1).join("");
}

translatePigLatin("california");

You are going to have to rethink your current algorithm. Your function returns lovegay instead of oveglay. Why? Because you are only taking the first character of str (str[0]). Your algorithm needs to look for consonant clusters and move them to the end and add ay after that.

Your current algorithm’s step 3-5 is the problem.