Words without vowels?(Pig Latin)

Words without vowels?(Pig Latin)
0

#1

Tell us what’s happening:

Your code so far


function translatePigLatin(str) {

  if(["a","e","i","o","u"].indexOf(str[0]) !== -1) {

        str += "way";

    } else { 

        while(["a","e","i","o","u"].indexOf(str[0]) == -1) {

           str += str[0];
           str = str.slice(1); 

        };

        str += "ay";
    };   

    return str;

};

  

console.log(translatePigLatin("cnsnnt"));


With the code above I’m not satisfying the “Should handle words without vowels.” parameter. When I enter all consonants it still works so I’m not sure what that means.

Your browser information:

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

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


#2

You have an infinite loop, because str[0] is 'c' everytime, so ["a","e","i","o","u"].indexOf('c') will always be equal to -1, so you stay inside the loop forever.


#3

Thanks for your reply, but I don’t understand how it makes an infinite loop. When i log any type of word it works perfectly. I’d think an infinite loop would throw an error or cause a crash no? Also, i tried copying and pasting the solution provided in the “Get a Hint” tab and also had the same results.


#4

I just reset the exercise, entered the same code, and it came out correct. I think there may be a bug on this one.


#5

If you are referring to the code in the Get a Hint link, then it should work. Your original code will not pass the challenge. Infinite loop kicks out early without a warning or error message on FCC curriculum currently.


#6

No I mean my original code. I tried it over again a few times just to make sure. It came out correct three times out of 7 or so. Seemed to work after i reset the exercise or opened a new window. The Get a Hint code did not work when i tried it. Must be something wrong on the page.


#7

but the code in the while loop is changing the string, so str[0] is different at each iteration, it doesn’t seem to be an infinite loop


#8

Actually, you are correct that it does change, but str never changes to anything that is not a string of consonants (hence the infinite loop). Where do you ever add a vowel onto str inside the while loop?

I think you should walk through your code using “cnsnnt” as str and you will see it can never escape.

Below are the first 10 values of what str looks like at the end of the first 10 iterations.

nsnntc
snntcn
nntcns
ntcnsn
tcnsnn
cnsnnt
nsnntc
snntcn
nntcns
ntcnsn

EDIT: You are close to a solution. You just need to think about the max number of iterations the while loop should ever run, before exiting. If str contains 100 consonants, then you would exit the loop, once you have iterated 100 times and add ‘ay’ on the end, because you know for sure, you have gone through the entire string str and there are no vowels.


#9

Interesting, would not have thought to do it out like that. Good to know that FCC doesn’t throw errors for infinite loops. Thanks for breaking it down for me!