Hey folks. how are you today?!
Today I saw a cool and interesting JavaScript challenge and thought I'd bring it to our community too!
Soo...
The Challenge π΅οΈββοΈ
The expected result for the challenge will be something like below:
π― Expected Output:
{
CJS: "This is the original Node.js module system, used since its first versions."
}
And also the base code is to start solving the challenge as below:
π― Base Code:
function getCorrectDescription(patternName) {
let patterns = {
ESM: "This is a more modern modularization pattern, officially introduced in newer versions of Node.js.",
CJS: "This is the original Node.js module system, used since its first versions.",
AMD: "AAMD is a modularization standard associated with JavaScript development for the browser.",
};
return ?;
}
A little cool, right?!
When I saw this challenge I thought it was really cool and I needed to take some time to apply it and finish it.
And you, what would be your proposed solution?! Leave it in the comments, let's share and see different ways of solving
See you ππ
Top comments (18)
See Computed Property Names
I'd ask questions about this "challenge":
getCorrectDescription
which implies string, but it's expected to return an object shaped similarly to the hard-coded patterns. Is this an oversight or intentional?patternName
is not found inpatterns
? Should it return an empty object ({}
)?without the
if(...)
you get:{ XYZ: undefined }
. Now you get just undefined which might cause trouble, if the result is expected to be an object.The return without a value definitely needs to be handled separately, and this if is just an example to avoid getting the original value of the patterns:
Hy,
whatΒ΄s wrong about this solution?
This returns a well formed object in any case.
Look at my code, you will find where the problem lies
I only see that your code delivers a strange result and that it is not able to deal with unknown values. Look here. What is it good for?
Maybe there's a problem with my expression. What I mean is that accessing attributes requires security checks. First, I will provide the code required for the question:
If I skip the If (...) check and inject the cracking method I provide into the environment, the internal data of the function will be exposed. For safety reasons, it's best to be careful
By the way, buddy, which country are you from; I come from China, nice to meet you; This is my first time communicating with foreign friends
Ok, I understand your point. But you need full access to the code anyway to use
Object.defineProperty
, so I would just call this programming, not hacking.For safety reasons it is good to always return an object of the same structure, otherwise you need to check if the name is null or a string in the following routines, which I would try to avoid.
Most people on dev.to name their country in the profile. ItΒ΄s nice to meet people all over the world on this place.
What about this?
Similar solution to Jaydeep Pipaliya, written in TypeScript.
return patterns?.[patternName]
...
return patterns.hasOwnProperty(patternName)
? { [patternName]: patterns[patternName] }
: null;
...
I take it you're not used to taking time off from work. As a family psychologist by profession, I would say that you are at risk of internal burnout