Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!