var countAndSay = function (n) { let result = '1' for (let i = 1; i < n; ++i) { let nextResult = '' let char = result[0] let count = 1 for (let j = 1; j < result.length; ++j) { if (result[j] === char) { ++count } else { nextResult += `${count}${char}` char = result[j] count = 1 } } result = `${nextResult}${count}${char}` } return result }