6__Duplicate_Encoder.js 569 B

1234567891011121314
  1. // The goal of this exercise is to convert a string to a new string
  2. // where each character in the new string is "(" if that character appears only once in the original string,
  3. // or ")" if that character appears more than once in the original string.
  4. // Ignore capitalization when determining if a character is a duplicate.
  5. function duplicateEncode(word) {
  6. let newStr = "";
  7. word = word.toLowerCase();
  8. for (let i = 0; i < word.length; i++) {
  9. if (word.split(word[i]).length > 2) newStr += ")";
  10. else newStr += "(";
  11. }
  12. return newStr;
  13. }