SO - 29886489
by tuga
JavaScript
var str = "HELLO";
var letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var arr = str.split(''),
//a temp array to store already replaced locations
temp = [],
pos;
for (var i = 0; i < 3; i++) {
//since you want 3 different chars to be replaced make sure the current position is not already replaced
do {
pos = Math.floor(Math.random() * arr.length);
} while (temp.indexOf(pos) > -1)
//replace the character at position pos in the array arr, the character to be replaced is randomly selected from teh letters string
arr[pos] = letters[Math.floor(Math.random() * letters.length)]
//store the current position in the temp array
temp.push(pos);
}
str = arr.join('');
alert(str);