Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda