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

124
Vistas
I need to transform a short piece of code that adds gaps to a phrase and have no idea how to do it

I have this piece of code that adds some gaps into a word/phrase like this:

"I drove everywhere by car" > "I d_o_e e_e_y_h_r_ _y c_r_"

How can it be transformed so it would to a result that is a little different:

"I drove everywhere by car" > "I d___e e___y___r_ b_ c__"

The difference is that it would show the first letter of every word, then make a 3-letter gap, and, if the word is long enough, show the 5th letter, another 3-letter gap and so on.

The piece of code:

var str = document.getElementById("dropped-v").innerHTML;
letters = '';
for (var i = 0, len = str.length; i < len; i++) { 
    if (i % 2 != 0 && str[i] != ' ') { 
        letters += '<span class="hint">_</span>'; 
    } 
    else {
       letters += str[i]; 
    } 
}
document.getElementById("dropped-v").innerHTML = letters;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here's one way to do it for simple sentences without periods. You can add period support and HTML injection as needed.

let MASK = '_';

function obfuscate_word(word) {
  const letters = [...word];
  const mask_letters = letters.map((letter, i) => i % 4 == 0 ? letter : MASK);
  return mask_letters.join('');
}

function obfuscate_sentence(sentence) {
  const words = sentence.split(' ');
  const mask_words = words.map(word => obfuscate_word(word));
  return mask_words.join(' ');
}

const s = "I drove everywhere by car"
console.log(s);
console.log(obfuscate_sentence(s));

If you want to, you can replace the MASK and get/set your HTML something like this:

MASK = '<span class="hint">_</span>';
const e = document.getElementById("dropped-v");
e.innerHTML = obfuscate_sentence(e.innerHTML);

This assumes that the inner HTML of the dropped-v element is a simple sentence (and not a previously-obfuscated sentence that includes markup, for example).

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