Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
for loop in countAndSay function keeps returning blank string after array join

I am working on the countAndSay leetcode question. I think my logic is correct, and I'm certain my code is almost there, but I keep getting a blank output on my return statement. I'm struggling to find where my code is causing this string to be blank. Could someone help me out?

The count-and-say sequence is a sequence of digit strings defined by the recursive formula:

countAndSay(1) = "1" countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string. To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.

/**
 * @param {number} n
 * @return {string}
 */
var countAndSay = function(n) {
    sayString = "1";
    sayArray = [1];
    root = 0;
    previousRoot = 1;
    count = 0;

    //begin master loop that creates sayString out of sayArray, run through it n times
    for (i = 0; i < n; i++) {

        //loop through the sayString as many times as there are characters to create a sayArray
        for (let i = 0; i < sayString.length; i++) {

            //take the first digit in the string call it root
            root = sayString[i];

            //if root equals previous root (1 on first loop),
            if (root == previousRoot) {
                // increase count variable by 1
                count++;
            }

            // else add the count to the sayArray, add the new root value to the array, set count to 0
            else {
                sayArray.push(count);
                sayArray.push(root);
                count = 0;
            }

            //change previous root to root so we can check its value next time through the loop
            previousRoot = root;
        }
        //make the new sayString a concatination of the sayArray
        sayString = sayArray.join("");
        //set the sayArray to empty so we can start popolating it again during the loop
        sayArray = [];
    }
    //after weve ran the major loop n times, return sayString
    return sayString;
};

console.log(countAndSay(4));

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda