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

135
Visualizações
Creating a function that returns a function with individual letters

Trying to create a function that returns a function and returned function will provide the individual characters of the string passed to the original function on its invocations. When the end of the string is met, it will start back at the beginning.

Example: 'cat' should return: 'c' (on the first invocation) 'a' (on the second invocation) 't' (on the third) 'c' (should then return back to beginning)

Here is the code that I have thus far:

function individualChar (str) {
let individualStr = str;
let invoked = 0;
return function () {
    if (invoked > 0) {
        let char = individualStr.slice(0);
        invoked++
        return `${char}`;
    }
    return individualStr;
}

}

My thought process was having the individualStr assigned to the str argument along with creating an 'invoked' counter. Then in the returned function, creating the 'char' variable and assigning that to the individualStr and slice it at the first character (0), to take it out, and then have an invoke counter to invoke the function as needed.

However, as of right now, it's still only returning the entire string.

I thought about maybe using a while loop as well, to keep track of the number of invocations, but I wasn't sure if that would be best approach for this.

Any guidance would be appreciated!

about 4 years ago · Santiago Gelvez
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