1. var countAndSay = function(n) {
    2. if (n===1) return "1";
    3. else {
    4. var str = countAndSay(n-1);
    5. var temp = str[0];
    6. var count = 0;
    7. var ans = '';
    8. for (var i=0; i<str.length;i++){
    9. if (str[i]===temp) count++;
    10. else {
    11. // ans += count.toString() + temp.toString();
    12. ans += '' + count + temp;
    13. temp = str[i];
    14. count = 1;
    15. }
    16. // if (i===str.length-1) ans += count.toString() + temp.toString();
    17. if (i===str.length-1) ans += '' + count + temp;
    18. }
    19. return ans
    20. }
    21. };

    image.png