Cannot pass the final test case on the pig latin challenge

Cannot pass the final test case on the pig latin challenge
0

#1

I’ve passed all test cases except for the last one - Should handle words without vowels. I keep getting the same error :


 Cannot read property '0' of null

Your code so far


function translatePigLatin(str) {
  var pigLatin = new String();
  var regex = /[aeiou]/gi;

  if(str[0].match(regex)) {
    pigLatin = str.concat('way');
  }
  else{
    var pigSub = str.indexOf(str.match(regex)[0]);
    pigLatin = str.substr(pigSub) + str.substr(0, pigSub) + 'ay';
  }

  return pigLatin;
}
translatePigLatin("consonant");

Your browser information:

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

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


#2

The problem is that the programming logic you picked handles words that contain vowels only.

Hint: What about those words that do not contain any vowel?

Think about that.


#3

I’ve also tried the following code but it still won’t pass the final test case.


 if(str.match(!regex)) {
    pigLatin = str.concat('way');
  }

I still get the same error message.


#4

@Stewart-Hetherington If the word has no vowels, add ay to the end. I’m not sure why that’s not in the instructions.


#5

The problem is in the Else statement:

else{
    var pigSub = str.indexOf(str.match(regex)[0]);
    pigLatin = str.substr(pigSub) + str.substr(0, pigSub) + 'ay';
}

It looks for the index of a vowel str.match(regex).

What if there was no vowel?

pigSub variable will end up empty or -1.
PigLatin too, because it uses pigSub.

Write a word that doesn’t contain any vowel, something like ‘tms’… (as an argument: translatePigLatin("tms");)

And then try to console.log(pigLatin). You’ll notice that nothing will be logged into the console.

That’s the problem.


#6

I’ve used your ‘tms’ input example and added an if statement to check whether the string has no vowels and it returns the correct string. However the error code - Should handle words without vowels still appears.


function translatePigLatin(str) {
  var pigLatin = new String();
  var regex = /[aeiou]/gi;

  if(str[0].match(regex)) {
    pigLatin = str.concat('way');
  }
  else{
    if(!str.match(regex)) {
      pigLatin = str.concat('way');
      //console.log(pigLatin);  // < This prints 'tmsway' but stilll fails the test case.
      return pigLatin;
    }
    var pigSub = str.indexOf(str.match(regex)[0]);
    pigLatin = str.substr(pigSub) + str.substr(0, pigSub) + 'ay';
  }

  return pigLatin;
}
translatePigLatin("tms");


#7

Very Close!

instead of way try ay:

Code:

pigLatin = str.concat('ay');


#8

Still no joy! I’m starting to think it might be a problem with the challenge itself.


function translatePigLatin(str) {
  var pigLatin = new String();
  var regex = /[aeiou]/gi;

  if( str[0].match(regex) || !str.match(regex) ) {
    pigLatin = str.concat('way');
  }
  else{
    if(!str.match(regex) ) {
    pigLatin = str.concat('ay');
    //console.log(pigLatin);
    return pigLatin;
  }
    var pigSub = str.indexOf(str.match(regex)[0]);
    pigLatin = str.substr(pigSub) + str.substr(0, pigSub) + 'ay';
  }

  return pigLatin;
}
translatePigLatin("tms");



#9

Your last code was fine.

You just had to change way to ay

But for this code now, you just need to delete the new added

|| !str.match(regex)

from your first if statement.

The code should be:

if( str[0].match(regex)) {
    pigLatin = str.concat('way');
  }

#10

Did you solve it?
or not yet?


#11

Yeah, finally! I just forgot to remove that line in the first if statement. Thanks very much, I really appreciate the help.


function translatePigLatin(str) {
  var pigLatin = new String();
  var regex = /[aeiou]/gi;

  if( str[0].match(regex)) {
    pigLatin = str.concat('way');
  }
  else{
    if(!str.match(regex) ) {
    pigLatin = str.concat('ay');
    return pigLatin;
  }
    var pigSub = str.indexOf(str.match(regex)[0]);
    pigLatin = str.substr(pigSub) + str.substr(0, pigSub) + 'ay';
  }

  return pigLatin;
}
translatePigLatin("tms");



#12

You’re welcome!

Good Luck & Happy Coding.