I'm trying to replace a percentage of letters in an array containing single letter strings. I'm trying to do it with a while loop but it doesn't seem to work here's what I've tried:
let validAnswer = false;
let neededHintLetters = Math.round(blankLetters.length * 40/100);
while(!validAnswer) {
let numHintLetters = 0;
$.each(blankLetters, function(i) {
let index = Math.floor(Math.random() * blankLetters.length);
if (index !== i) {
blankLetters[i] = '?'
}
else { //don't replace the letter
numHintLetters++;
}
});
if (numHintLetters === neededHintLetters) {
break;
}
}
Ok I changed the way I thought about it lol I don't know why I was trying with these kinds of loops... possibly because it was super late yesterday Here is what worked for me:
const NUM_HINT_LETTERS = Math.ceil(blankLetters.length * 50/100); //ceil.50% of the words should stay visible as hints
let hintPlacement = []; //array that matches the letters
$.each(blankLetters, function(i) {
let hint;
if (i < NUM_HINT_LETTERS - 1) {
hint = true; //the first NUM_HINT_LETTERS of elements are true and wont be changed to a blank space
}
else {
hint = false;
}
hintPlacement.push(hint);
});
shuffle(hintPlacement); //give the hints random positions
$.each(blankLetters, function(i) {
if (!hintPlacement[i]) {
blankLetters[i] = '?'
}
});