DEV Community

Discussion on: #2 Live Kata Report

Collapse
forethought_de profile image
Karthikeyan P • Edited on

Hello Raffaele,

Here is my solution :) BTW, I really like, how you solved with array destruction. Well done :)

const strManipulation = (str) => {
  let strArr = str.split(' ');
  const appendStr = 'ay';
  let finalText = "";
  let regex = new RegExp('[^a-zA-Z\d\s: ]', 'g');

  strArr.forEach((item) => {
    let nonAlphabetStartIndex = item.search(regex);
    if(nonAlphabetStartIndex){
        finalText += `${item.slice(1, nonAlphabetStartIndex)}${item.charAt(0)}${appendStr}${item.slice(nonAlphabetStartIndex)} `;
    }
    else{
      finalText += `${item.slice(1)}${item.charAt(0)}${appendStr} `;
    }
  });

  return finalText.trimEnd();

};