function f(str) { let count = 0; let findCount = function (str) { for (let i = 0; i < str.length - 1; i++) { if(str[i] === str[i+1]) { str = str.slice(0, i) + str.slice(i+2); count++; return findCount(str); } } return; } findCount(str); console.log(count); } f('12344321')