Insert character after every n characters
Write a function that takes two strings (a and b) as arguments. Start with the end of ‘a’, insert ‘b’ after every 3rd character of string ‘a’.
const insertChart = (str, c) => { let resultStr = ''; let index = 0; for(let i = (str.length - 1); i > -1 ; i--) { index++; resultStr = str[i] + resultStr; if(index % 3 === 0) { resultStr = c + resultStr; } } return resultStr; }; const input = 'zxyzxyzxyzxyzxyz'; const inputChar = '.'; console.log(insertChart(input, inputChar));