I'm trying to make it so that text will be shown under the input box with some of the letters should be replaced with underscores. This text will be the answer that the user should type in to get the correct output.
I expected the loop to replace letters like e, with the _, but it does nothing. There are no error messages, it just doesn't replace the letter e. I have tried to put a for loop, which I found on other places for how to replace certain characters from an array's element, but nothing worked.
Here is the JS code:
let array = ['blue', 'green', 'red', 'orange', 'pink', 'silver', 'purple', 'lime']
let arraySecond = ['blue', 'green', 'red', 'orange', 'pink', 'silver', 'purple', 'lime'];
//The other array is the one that shows the element,
//that matches the first array, but with "_", instead of certain letters
document.getElementById('check').addEventListener('click', thisFunction);
function thisFunction() {
let value = document.getElementById('input').value;
if (value == theColor) {
alert(`Congrats you gussed right, its ${value}`)
} else {
alert(`Oof, your guess was incorrect, it\'s ${array[randomizedArray]}`)
}
}
let randomizedArray = Math.floor(Math.random() * array.length);
let theColor = array[randomizedArray];
//One thing the code above is used for is be in the for loop, to replace letters
for (let i = 0; i < theColor.length; i++) {
theColor[i] = theColor[i].replace('e', '_');
}
//This loop that should do the replacing, but it doesn't work
document.getElementById('output').innerHTML = theColor;
e(for example) with_, or be able to decide whcih ones to replace?es it finds or just one?