Search and Replace. I'm stuck, need help!

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/

  1. Yes. It’s more like tCase is truthy not enter ‘tcase’ value.

  2. Yes. You don’t have to necessarily enter it but it’s based on existing value of tCase

  3. Looks like you understand the top 2 functions. function getCase basically compares incomings string to see if it’s a uppercase/lowercase, etc… then simply return strings.

  4. It’s called destructuring introduced from the newer ES6 syntax.

Cheers :slight_smile:

1 Like

Thanks, but I still wonder in the following code, can you explain to me:

return {
        textCase
};
// This will return an object as follows: { textCase: "string"}. Am i right?

So what do the following code do?

const { textCase } = Util; // I don't know what the result of this code is
const replacingStr = textCase(after, textCase(before)); // The way the above function works with 2 parameters is given, can you tell me how it works?
  1. Yes, it will return an object with that key. It’s shorthand for
return {
  textCase: textCase
}
  1. It basically means const textCase = Util.textCase;

textCase eventually returns a string. Because 2nd parameter is passed, it will call setCase method which then will return either uppercased/lowercased/capitalized word.

1 Like

i got it, thank you very much !!!

This is destructuring, a JavaScripr ES6 feature

1 Like