Tell us what’s happening:
I finished the test, but when I looked at other methods, I encountered a code that confused me:
Your code so far
// Add new method to the String object, not overriding it if one exists already
String.prototype.capitalize = String.prototype.capitalize ||
function() {
return this[0].toUpperCase() + this.slice(1);
};
const Util = (function () {
// Create utility module to hold helper functions
function textCase(str, tCase) {
// Depending if the tCase argument is passed we either set the case of the
// given string or we get it.
// Those functions can be expanded for other text cases.
if(tCase) {
return setCase(str, tCase);
} else {
return getCase(str);
}
function setCase(str, tCase) {
switch(tCase) {
case "uppercase": return str.toUpperCase();
case "lowercase": return str.toLowerCase();
case "capitalized": return str.capitalize();
default: return str;
}
}
function getCase(str) {
if (str === str.toUpperCase()) { return "uppercase"; }
if (str === str.toLowerCase()) { return "lowercase"; }
if (str === str.capitalize()) { return "capitalized"; }
return "normal";
}
}
return {
textCase
};
})();
function myReplace(str, before, after) {
const { textCase } = Util;
const regex = new RegExp(before, 'gi');
const replacingStr = textCase(after, textCase(before));
return str.replace(regex, replacingStr);
}
So the problems I encountered are:
1. If(tCase) {
return setCase(str, tCase);
} else {
return getCase(str);
}
I understand the above code as follows: if we enter ‘tcase’ value, the function will return setCase(str, tcase) and similar to the else statement. Is that the correct way to understand? If I get it wrong, please tell me.
2. function setCase(str, tCase) {
switch(tCase) {
case "uppercase": return str.toUpperCase();
case "lowercase": return str.toLowerCase();
case "capitalized": return str.capitalize();
default: return str;
}
}
If I understand correctly case "uppercase": return str.toUpperCase();
that means that when we enter the value of tCase with “uppercase” it will return str.toUpperCase (); and similar to other cases. Is that the correct way to understand? If I get it wrong, please tell me.
3.
Through two questions 1 and 2, can someone explain to me how the following code works in the most detailed way?
const Util = (function () {
// Create utility module to hold helper functions
function textCase(str, tCase) {
// Depending if the tCase argument is passed we either set the case of the
// given string or we get it.
// Those functions can be expanded for other text cases.
if(tCase) {
return setCase(str, tCase);
} else {
return getCase(str);
}
function setCase(str, tCase) {
switch(tCase) {
case "uppercase": return str.toUpperCase();
case "lowercase": return str.toLowerCase();
case "capitalized": return str.capitalize();
default: return str;
}
}
function getCase(str) {
if (str === str.toUpperCase()) { return "uppercase"; }
if (str === str.toLowerCase()) { return "lowercase"; }
if (str === str.capitalize()) { return "capitalized"; }
return "normal";
}
}
return {
textCase
};
})();
4. The last question is about variable declaration as follows:
const { textCase } = Util;
Why is there the occurrence of {}. Is the above code different from the usual declaration?
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/search-and-replace/