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("(( @")
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;
}