I was tasked with writing a function that takes a string and obfuscates it. For all ‘a’ in a strong , they are converted to a 4, 'e’s go to 3, ‘o’ to 0, and ‘l’ to one. I have the correct answer, however I an having trouble wrapping my head around the functionality.
myArgs = process.argv.slice(2);
var string = myArgs[0];
function obfuscate(string) {
var newString = string.replace(/a/g, "4");
newString = newString.replace(/e/g, "3");
newString = newString.replace(/o/g, "0");
newString = newString.replace(/l/g, "1");
return newString;
}
console.log(obfuscate(string))
The word password, for example, would cone out, p4ssw0rd
Any simplified explanation to this code would be greatly appreciated.
See my comments below in your original code.
connoror:
myArgs = process.argv.slice(2);
var string = myArgs[0]; // assuming the first argument in myArgs is a string to obfuscate, this line assigns the string to a variable named string
function obfuscate(string) {
var newString = string.replace(/a/g, "4"); // replaces all instances of lower case 'a' to '4'
newString = newString.replace(/e/g, "3"); // replaces all instances of lower case 'e' to '3'
newString = newString.replace(/o/g, "0"); // replaces all instances of lower case 'o' to '0'
newString = newString.replace(/l/g, "1"); // replaces all instances of lower case 'l' to '1'
return newString;
}
console.log(obfuscate(string)) // displays the result of calling the function with the **string** variable as the argument.