Reputation: 49
I need to have the right function to test, which is giving me string from a fullName and which I have to split and using for loop change the letters to a char "*". I want to keep the first upperCase visible: here is my code:
var fullName = "Jozko Baci";
function testSamoUloha(fullName) {
var splitString=fullName.split("");
for (var i=1;i<splitString[1].length;i++) {
splitString[1].replace(splitString[1][i],"*");
}
var anonymName =splitString[0]+" "+splitString[1];
console.log(anonymName);
}
testSamoUloha();
I'm really new to it, this problem took me two hours to have at least some solution.
I expect that from the string above will become string saved in variable anonymName as "Jozko B***";
Upvotes: 4
Views: 143
Reputation: 5343
You can use the following regex if you'd like to make your code shorter:
(?<=^.+\s\S+)\w
it will match any letter \w
that is preceded (?<=
by start of string ^
, any number of chars .+
, a whitespace \s
and one or more non-whitespace characters \S+
const fullName = "Jozko Baci";
const censored = fullName.replace(/(?<=^.+\s\S+)\w/g, '*');
console.log(censored);
Upvotes: 1
Reputation: 4557
Could do it with something like this;
const fullName = "Jozko Baci";
// Break name into words (firstName, lastName)
let res = fullName.split(' ').map((word, wordIndex) =>
// If after firstName
wordIndex > 0
// Hide all but first character in word
? word.split('').map((char, charIndex) => (charIndex > 0 ? '*' : char)).join('')
// Else, show entire word
: word
).join(' '); // Join words back together (firstName, lastName)
console.log(res);
Hope this helps,
Upvotes: 0
Reputation: 5260
The above code looks fine, just added a fix in this line
splitString[1].replace(splitString[1][i],"*")
Also added fix to var splitString=fullName.split(" ");
replaced
fullName.split(""); // ['J', 'o', 'z', 'k', 'o', ' ', 'B', 'a', 'c', 'i']
with
fullName.split(" "); // ['Jozko', 'Baci']
if you use replace on a string it returns the new string after replace , its is not rewriting the existing string
for eg:
var a = "hello";
a.replace('o', '*') // returns hell*
a // has hello
but if you do
a = a.replace('o', '*') // returns hell* and rewrites a
a // hell*
similarly after adding this fix the existing code works fine
var fullName = "Jozko Baci";
function testSamoUloha(fullName) {
var splitString=fullName.split(" ");
for (var i=1;i<splitString[1].length;i++) {
splitString[1] = splitString[1].replace(splitString[1][i],"*");
}
var anonymName =splitString[0]+" "+splitString[1];
console.log(anonymName);
}
testSamoUloha(fullName);
Upvotes: 0
Reputation: 63524
Strings are immutable (you can't change them) so this code:
splitString[1].replace(splitString[1][i],"*");
won't work in any situation.
What I suggest is you create a temporary string, then loop over the whole of your second word. If the index of the loop is 0 add the letter to the temporary string, otherwise add a *
:
var fullName = "Jozko Baci";
function testSamoUloha(fullName) {
var splitString = fullName.split(' ');
// Create a temporary string
var tempString = '';
// Loop over the whole second word
for (var i = 0; i < splitString[1].length; i++) {
// If the index is greater than zero (not the first letter)
// add a * to the temporary string
if (i > 0) {
tempString += '*';
// otherwise, if the index is 0, add the letter
// to the temporary string instead
} else {
tempString += splitString[1][i];
}
}
// Return your string from your function
return splitString[0] + ' ' + tempString;
}
// Make sure you pass in the fullName as an argument
console.log(testSamoUloha(fullName));
Upvotes: 0