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;
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).