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

195
Visualizações
How to count backwards

Hey guys im developing something and I need to fix this please give me some feedback on how to improve this. (how to make it cleaner, easier to read , etc). Here's the code!

function wordFliper(word) {
    let wordOutput = "";
    let reverseIndex = word.length - 1;
    for (let index = reverseIndex; index >= 0; index--) {
      let storage = word[index];
      wordOutput = wordOutput + storage;
    }
    return wordOutput;
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use JS array functions like reduce(),split(),reverse(), etc. You can write the code in following ways:

  1. function wordFlipper(word) {
        return(word.split('').reverse().join(''));
    }
    
  2. function wordFlipper(word) {
        return(word.split('').reduce((a,c)=>c+a));
    }
    

Please go through the links for clarity on the functions used above:

  • split() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
  • join() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
  • reverse() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse
  • reduce() - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
about 4 years ago · Juan Pablo Isaza Relatório

0

I would remove some variables (variables that are used once in loop and then assigned to a function variable) to reduce code and rename one like this:

/// <summary>Function to flip words backwards</summary>
/// <param name="word" type="string">Word to be flipped</param>
/// <returns type="string">Word flipped</returns>

function wordFlipper(word) {
    let flippedWord = "";
    for (let i = word.length - 1; i >= 0; i--) {
      flippedWord += word[i];
    }
    return flippedWord;
}

Also IMHO use i variable instead of index (for loop incrementer)

And Also get used to commenting your code, to know what is for the functions you're writing

I hope you helped to keep your code clean for further programming and happy coding!

about 4 years ago · Juan Pablo Isaza Relatório
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