The issue with your code is that in for loop everytime youare trying to replace on "Hello World" string and assign the result to newStr. However in the last iteration you check for u, since u is not presetn in Hello World, the entire string gets assigned as such to newStr.
You should instead initialise newStr to "Hello World" and then perform replace on it
var newStr = 'Hello World';
for (const c of "aeiou") {
console.log(c);
newStr = newStr.replace(c, '*');
}
console.log(newStr);
However note that this will only replace one instance of the matching character and not all of them, You will still need to use regex
newStr = newStr.replace(new RegExp(c, 'g'), '*');
var newStr = 'Hello World';
for (const c of "aeiou") {
newStr = newStr.replace(new RegExp(c, 'g'), '*');
}
console.log(newStr);
or split and join the string
newStr = newStr.split(c).join('*');
var newStr = 'Hello World';
for (const c of "aeiou") {
newStr = newStr.split(c).join('*');
}
console.log(newStr);
There is a however proposal for using
String.prototype.replaceAll
on string