Pig Latin_How can I fix my code?

Pig Latin_How can I fix my code?
0

#1

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


#2

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

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


#3

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


#4

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.


#5

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


#6

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?)


#7

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.


#8

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.


#10

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


#11

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");

#12

Do you understand what your solution is returning for test case #3?

If not, make sure to use console.log statements before each return statement and check your browser’s console to see what is being returned.

You could put the following line before the 2nd return statement.

console.log(newWord.join('').concat("ay"));

#13

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


#14

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


#15

I will give you a little hint. Your for loop is only making one iteration before your 2nd return statement forces the function to exit.


#16

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


#17

Your code’s problem has nothing to do with your break statement. I actually gave you a really big hint in my last reply. Remember, when the return statement executes, the function exits and for loops are not completed.


#18

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");