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

132
Vistas
Switch characters progressively to another in a string

How can I replace all caracteres by another with all possibilities ?

The function switch progressively all the characters by another.

const globalReplace = (chain) => {
    let d = 0;
    for (let i = 0; i <= chain.length - 1; i++) {
        for (let j = 0; j <= d; j++) {
            console.log(chain.slice(0, chain.length - 1 - i), 'X', chain.slice(chain.length - i, chain.length + 1));
        }
        d++;
    }
    return;
};

globalReplace('ABCDE');

the result is :

ABCDX ABCXE ABCXE ABXDE ABXDE ABXDE AXCDE AXCDE AXCDE AXCDE XBCDE XBCDE XBCDE XBCDE XBCDE

but i would like :

ABCDX ABCXE ABCXX ABXDE ABXXE ABXXX AXCDE AXXDE AXXXE AXXXX XBCDE XXCDE XXXDE XXXXE XXXXX

Have you any ideas ? I think my function is close to the result.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As you mentioned, order of output strings is not important. Keeping that in mind, I've written a code in python (I'm not well versed with js). You can translate it as you need.

def global_replace(chain, r_chr): # chain is the string to replace, r_chr is character (in your example, 'X')
    replaced_strings = []
    for i in range(0, len(chain)):
        r_str = r_chr * (i+1)          # this creates a replace str of length (i+1) with repeated r_chr
        for j in range(0, len(chain)-i):
            s = chain[:j] + r_str      # slice and replace portion of "chain" with r_str
            if j+i+1 < len(chain):     # string index error check
                s += chain[j+i+1:]
            replaced_strings.append(s)
    return replaced_strings 
about 4 years ago · Santiago Trujillo 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