Codewars - Simple Pig Latin

Problem:

Move the first letter of each word to the end of it, then add “ay” to the end of the word. Leave punctuation marks untouched.

My code currently:

function pigIt(str){
  let array = str.split(" ");
  console.log(array)
  let newStr = "";
  for (let i = 0; i < array.length; i++){
  array[i] = array[i].slice(1) + array[i].charAt(0) + "ay "; 
  newStr += array[i]
  } return newStr
}

let test = "This is my string";
console.log(pigIt(test));

Trying to create a new string from the array elements after FOR loops has been applied… any tips?

It looks like you’re forgetting to add the spaces back in. And if I understand the problem, the punctuation marks should stay where they are at? Personally, whenever I use split in a problem like this, I anticipate needing to use it’s sibling join method.

Even if I did use the JOIN method i.e got rid of line 6 and used join method when returning newStr … still yields the same issue.

function pigIt(str){
  let array = str.split(" ");
  console.log(array)
  let newStr = "";
  for (let i = 0; i < array.length; i++){
  array[i] = array[i].slice(1) + array[i].charAt(0) + "ay "; 
  } return newStr.join('')
}

the result should be a string in quotation marks

split works on strings. join works on arrays. newStr.join(’’) won’t give the desired result, because it’s trying to join something that is already joined.

Try putting the results of the for loop in an empty array instead of an empty string. If nothing else changes, there should still be 1 or 2 more minor changes you’ll have to take care of.

thats my mistake I didn’t paste the updated code :

function pigIt(str){
  let array = str.split(" ");
  console.log(array)
  let newArr = []
  newStr = "";
  for (let i = 0; i < array.length; i++){
      newStr = array[i].slice(1) + array[i].charAt(0) + "ay"; 
      newArr.push(newStr);
    } return newArr.join(" ")
}

Now the problem asks for all characters i.e non alpha numeric characters to be left alone and obviously not appended with “ay”

try to make some sample things

if you have input string "Hello, World!" your code returns "ello,Hay orld!Way"
how do you avoid that?

if there is no specification I think you can ignore lower case and upper case letters

but the comma should be after the word not in the middle of it