Pig Latin_How can I fix my code?

Tell us what’s happening: Can someone help me with this code? It’s passing only the first two tests, and my regex and for-loop don’t seem to be working

Your code so far


function translatePigLatin(str) {
  var regex = /[aeiou]/;
  let arr = str.split('');
  let temp = [];
  if (arr[0] === regex) {
    return arr.join('').concat("way");
  } else {
    for (let i = 0; i < arr.length; i++){
      let count = 0;
      if (arr[i] !== regex) {
        temp.push(arr[i]);
        count++;
      } else {
        break;
      }
      let newWord = arr.slice(count).concat(temp);
      return newWord.join('').concat("ay");
    }
  }
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

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

so your regex line:
var regex = /[a-e]/;

it matches all the lowercase letters a,b,c,d,e ?
Is that what you wanted?

Oh, I just fixed it so it says [aeiou].

if (arr[0] === regex)

This line won’t work. You need to either use match function or test function to compare a character to a regex.

Having fixed that but still not passing the other tests, I think my for-loop is the issue.

What are you expecting this if statement to do?

for eg. if arr[0] is the letter ‘a’, what do you expect the if to do with it and what do you expect the result of this will be? (true or false?)

I would like the vowel-fronted word to concatenate with ‘way’.

I just tried changing (arr[0] === regex) to (arr[0].test(regex)). Now, I’m not passing any tests.

well at least you have the right idea. You just used the test incorrectly.
Try this
if (regex.test(arr[0])) {

I do suggest after you fix that line, that you do exactly what I’m doing on your behalf, that is, go back and read your code, line by line and ask yourself if each line of code is doing something useful and if so what. Is it really working as you were expecting?
Then if you think your code is really good, then add console.log statements to try to identify further issues.

I’m avoiding looking at solutions to the problem, yet it’s hard not to be tempted.

1 Like

My solution now is passing tests 1, 2, 4, and 5.

function translatePigLatin(str) {
  var regex = /[aieou]/;
  let arr = str.split('');
  let temp = [];
  if (regex.test(arr[0])) {
    return arr.join('').concat("way");
  } else {
    for (let i = 0; i < arr.length; i++){
      let count = 0;
      if (!regex.test(arr[i])) {
        temp.push(arr[i]);
        count++;
      } else {
        break;
      }
      let newWord = arr.slice(count).concat(temp);
      return newWord.join('').concat("ay");
    }
  }
}

translatePigLatin("consonant");
1 Like

I’m getting ‘lovegay’ for ‘glove’. The for-loop doesn’t seem to be running.

I think it is running… but you should add some console.log statements in there to log what is happening to the array…

I was trying to create a break statement that kicks in only when regex.test(arr[i]) equals true…

Oh yeah! I just got it. Thanks!

function translatePigLatin(str) {
  var regex = /[aieou]/;
  let arr = str.split('');
  let temp = [];
  let count = 0;
  if (regex.test(arr[0])) {
    return arr.join('').concat("way");
  } else {
    for (let i = 0; i < arr.length; i++){
      if (!regex.test(arr[i])) {
        temp.push(arr[i]);
        count++;
      } else {
        break;
      }
    }
    let newWord = arr.slice(count).concat(temp);
    return newWord.join('').concat("ay");
  }
}

translatePigLatin("consonant");

that’s genius… i wouldn’t be able to figure this out