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.
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.