Iāve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the āpreformatted textā tool in the editor (</>) to add backticks around text.
Ok, hereās my limited understanding of whatās going on:
You create a string method. Easy.
Then you create an IIFE, assigned to the variable Util.
I donāt understand the textCase function. How are the arguments str and tCase being supplied and how is tCase being assessed as true or false?
When that executes, it either invokes setCase or getCase.
The switch in setCase I donāt understand. I though that switch statements required breaks? I just donāt get how that makes a decision about which of the return commands to execute.
Where is the str argument getCase coming from? When it checks the status of str it returns the appropriate case label for the setCase function?
And what is return ānormalā? The string ānormalā doesnāt appear anywhere else in the code.
What does return { textcase } do?
Ok, then you have the actual replace function itself.
const {textcase} = Util is an example of destructuring assignment? What does it do exactly?
The regex thing I understand.
Finally, replacingStr is the textCase function which passes after and then a nested version of itself which passes before?? I just donāt get how this works either.
My solution to the same challenge used a simple regex and ternary operator and the entire function is three lines of code. The solution above seems (to my novice coder brain) wildly over-engineered!
FWIW, The final āsolutionā (no.5) provided in the guide is actually wrong and doesnāt even pass the challenge!
In fact, other provided solutions are also arguably inaccurate, in that they actually go beyond the criteria of the challenge, but donāt actually fail in any cases.
The criteria stipulate that the initial character of the replacing word should match the case of the initial character of the word being replaced. IIRC, a couple of the solutions iterate through the entire word, matching case of all characters.
My issue with these solutions is that they either go further than required, are so complex that they cover methods way beyond what has been already covered in the course, or that theyāre actually just plain wrong (i.e. no.4).
That said, Iāve found solutions to other challenges to be really useful and instructive on the whole.
As for my solution, itās about as basic as it gets and uses only methods which have been fully covered in the course. The regex /^[A-Z]/ is way simpler than many others covered in that section of the course.
Iāll say again, most beginners do not find regex simple or easy to understand. Itās cool that you like regex, but itās a common source of confusion and frustration.
Yes, sure, I understand. I just figured that using a regex was an obvious approach as itās a method which had been covered in considerable detail earlier in the course, thatās all.
I certainly agree that regexes can be pretty confusing at times though!
Ok, I think Iāve almost got my head around this now.
The one thing I just donāt quite understand is:
return { textCase }
This creates an object with a single key/value pair (textCase: textCase) but I donāt understand exactly why itās required? When const textCase is defined within the myReplace function, does that alone not allow you to call upon the textCase function within Util? Iām sure itās a really basic failure of understanding about how these things work but if someone could explain it to me, it would be really helpful!