Words without vowels?(Pig Latin)

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

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.

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

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.

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

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!