Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a word in a string and replace letters with different characters in JS

I’m trying to make a function which takes a random word and then splits the word into individual letters, then replace the letters with different characters to hide the word.

This is what I thought of originally, but does not work at all (selectWord and answer are both declared outside of all functions):

function word() {
selectWord = answer.split('').map(letter , " _ ").join('');

document.getElementById('correct-word').innerHTML = selectWord;}

Any input would be greatly appreciated 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

function word() {
    selectWord = answer.split('').map(() => " _ ")).join('');
    
    document.getElementById('correct-word').innerHTML = selectWord;
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading