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

140
Visualizações
Counting the occurrence of characters of a string, and then substituting them with ( or ) dependant on count logic

I need to get the string passed to the function to be split in to characters, then if the count of characters is equal to 1 in that word output a ( symbol, else ) my first Code Wars project. I can split the string into characters but I can't get the push method to work on the new array output, grabbed the count[str] ? count[str]++ : count[str] = 1; code from here.


  function duplicateEncode(word){ 
    for (i=0; i < word.length; i++){
      var count = 0;
     
      word.split("").forEach(function(str) {    
         count[str] ? count[str]++ : count[str] = 1;
         
         if (count ==1){
            output.push["("];
         } else{
            output.push[")"];
         }
         console.log(output);
      });
    } 
  }

  duplicateEncode("din")
  duplicateEncode("recede")
  duplicateEncode("Success")
  duplicateEncode("(( @")

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

first count each charcter Occurance, then map on the word again to compare OccuranceCount and replace with encode character

function duplicateEncode(word){
  characterOccurance = {} // will be {"char": "number of occurance"}

  word.split('').forEach(char => {
     if(characterOccurance[char]){ // if exist 
       characterOccurance[char] += 1; // increase the occurance count
     }else{
       characterOccurance[char] = 1;// set occurance to one
     }
  })

  let output = word.split('').map(
                    char => characterOccurance[char] == 1 ? ')' :'(' 
                    ).join('');
  return output;

}

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