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

163
Vistas
Duplicate Encoder JavaScript, try to use indexOf but the output is still incorect

convert a string to a new string where each character in the new string is "(" if that character appears only once in the original string, or ")" if that character appears more than once. can't find where i make a mistake

  1. "din" => "((("

  2. "recede" => "()()()"

  3. "Success" => ")())())"

  4. "(( @" => "))(("

    const duplicateEncode = (word) => {
     let newString = ''; 
      [...word.toLowerCase()].filter((e, i) => {
       if (word.indexOf(e) !== i) {
          newString += ')';
        } else if (word.lastIndexOf(e) !== i ) {
          newString += ')';
        } else newString += '(';
      });
    return newString;
    }
    
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You issue seems to be that you're using .indexOf() and .lastIndexOf() on word, which contains both uppercase and lowercase letters, but e will always be the lowercase character from your input, which results in .indexOf() and .lastIndexOf() not being able to find the letter when it's upercase. Instead, store the lowercase version of your input in a new variable and use that when you call .indexOf()/.lastIndexOf():

const duplicateEncode = (word) => {
  let newString = '';
  const lowerWord = word.toLowerCase();
  [...lowerWord].forEach((e, i) => {
   if (lowerWord.indexOf(e) !== i) {
      newString += ')';
    } else if (lowerWord.lastIndexOf(e) !== i ) {
      newString += ')';
    } else newString += '(';
  });
  return newString;
}

console.log(duplicateEncode("din")); // "((("
console.log(duplicateEncode("recede")); // "()()()"
console.log(duplicateEncode("Success")); // ")())())"
console.log(duplicateEncode("(( @")); // "))(("
console.log(duplicateEncode("nGnnI)nPne@uwJ")); // ")())(()()((((("

You should also use .forEach() instead of .filter() as you're not filtering and using the array that .filter() returns.

Here is another appraoch that involes creating a Map (similar to an object), that holds the frequency for each char as a value. Using .replace() we can return a new string where we replace each character based on if it appears multiple times or not:

const duplicateEncode = (word) => {
  const lower = word.toLowerCase();
  const charFreq = [...lower].reduce((map, c) => map.set(c, (map.get(c) ?? 0) + 1), new Map);
  return lower.replace(/./ug, (c) => charFreq.get(c) === 1 ? "(" : ")");
}


console.log(duplicateEncode("din")); // "((("
console.log(duplicateEncode("recede")); // "()()()"
console.log(duplicateEncode("Success")); // ")())())"
console.log(duplicateEncode("(( @")); // "))(("
console.log(duplicateEncode("nGnnI)nPne@uwJ")); // ")())(()()((((("

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