simple pig latin codewars kata problem

I’m doing this kata.

function pigIt(str) {

const words = str.split(" ");

for (var i = 0; i < words.length; i++) {

if (words[i] === "!") {

return "!";

}

var firstLetterSliced = words[i].slice(0, 1);

var remainingLetterSliced = words[i].slice(1, words[i].length);

return (remainingLetterSliced + firstLetterSliced + "ay");

}

}

pigIt('Pig latin is cool'); // igPay atinlay siay oolcay

My code isn’t passing a test. Please help fix the issue.

It works as expected so why is it not passing the tests?

Your only processing the first word. You’ve done it right but you need to do it to every word, rebuild the string and return it. If you thought you where doing every word then check your for loop and where the return is. I could tell you more but then the exercise would be pointless :stuck_out_tongue_winking_eye:

please tell more…:slight_smile: It won’t be pointless.

Im not sure how to give you more help without giving you the answer.
Your function is supposed to return…
‘igPay atinlay siay oolcay’
…but instead you returned…
‘igPay’
…why? Your function is returning a result before doing all the work, why? You know it did one word right, why not the rest? First I would put a console.log(words) after splitting str to see what you get, which would be [ “Pig”, “latin”, “is”, “cool” ] so you know thats right and you got all the words. Your for loop is told to do all the words so thats not it. The second word isnt a “!” so it shouldnt return there, but I would be asking my self why would I want the result of this function to be “!” at any point…If a word is “!” then the returning result will be “!”, is that what you want?. The last 3 lines successfully split the word and put it back together again, the result showed us that but the last of those 3 lines starts with a return so the function is going to stop what its doing and return the result of the last line, but thats not what you want. So instead maybe you should store that word somewhere and not return so the for loop can continue and process the rest of the words. You could put the result in another variable, say “result” and keep adding to that to build the resulting string. Or, since words is an array already with the right amount of words just put the result back in to words[i] and join that array later. When you hit “!” you dont want to return that but ignore it, so instead of returning you could continue. Look it up, continue in a for loop will make it skip this rest of the code in the for loop and got to its next iteration. Then after the for loop words should be filled with the modified words so you can join the array with a space and return that.
Ok, thats my attempt at giving you an answer without giving you code :stuck_out_tongue_winking_eye:

EDIT: If you want something to aim for, using modern stuff I got this down to one line of code no hassle. Thanks for reminding me of this place, thinking I might do a bunch again but this time try to force myself to use modern stuff…Im still very much a for loop guy, years of muscle memory to reprogram. Oh, and if you ever get to the point your just want to see a code answer, say and Ill give you a for loop version.

1 Like

If you want practice on a similar exercise that still introduces similar concepts with a video explanation, then this may be right up your alley as well:

You keep suggesting things to try and I dont bother, so I did this one.
Not comfortable using regex’s so I just used normal stuff.

  let devowelize = (str) => [...str].filter(letter => !'iouae'.includes(letter.toLowerCase())).join('');
  devowelize("Kirupa Chinnathambi");

Do I get a cookie? :stuck_out_tongue_winking_eye:

1 Like

Even better. You get a shiny badge :slight_smile:

1 Like