DEV Community

Cover image for JavaScript Challenge: What would be your solution to this challenge? πŸ₯·πŸ»πŸ§©
Renan Ferro
Renan Ferro

Posted on

JavaScript Challenge: What would be your solution to this challenge? πŸ₯·πŸ»πŸ§©

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...

Image description


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."
}
Enter fullscreen mode Exit fullscreen mode

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 ?;
 }
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
efpage profile image
Eckehard • Edited
return  {[patternName] : patterns[patternName]}
Enter fullscreen mode Exit fullscreen mode

See Computed Property Names

Collapse
 
moopet profile image
Ben Sinclair

I'd ask questions about this "challenge":

  1. The function is called 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?
  2. What should happen if patternName is not found in patterns? Should it return an empty object ({})?
Collapse
 
kiliaosi profile image
wangzhi
if (patterns.hasOwnProperty(patternName)) {
  return {[patternName] : patterns[patternName]}
} 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
efpage profile image
Eckehard

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.

Collapse
 
kiliaosi profile image
wangzhi

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:

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{[patternName] : patterns[patternName]};
 }


 Object.defineProperty(Object.prototype, 'hack', { get: function() { return this } })


 const result = getCorrectDescription('hack')

 console.log(result)

 /**
  {
  hack: {
    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.'
  }
}
  */
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
efpage profile image
Eckehard

Hy,

whatΒ΄s wrong about this solution?

...
return  {[patternName] : patterns[patternName]??"No description available"}
...
getCorrectDescription('hack') 
// ->{ hack: 'No description available' }
Enter fullscreen mode Exit fullscreen mode

This returns a well formed object in any case.

Thread Thread
 
kiliaosi profile image
wangzhi

Look at my code, you will find where the problem lies

Thread Thread
 
efpage profile image
Eckehard • Edited

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?

Thread Thread
 
kiliaosi profile image
wangzhi

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 (patterns.hasOwnProperty(patternName)) {
  return {[patternName] : patterns[patternName]}
} 

return {[patternName] : null}
Enter fullscreen mode Exit fullscreen mode

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

Thread Thread
 
kiliaosi profile image
wangzhi

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

Thread Thread
 
efpage profile image
Eckehard

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.

Thread Thread
 
efpage profile image
Eckehard

Most people on dev.to name their country in the profile. ItΒ΄s nice to meet people all over the world on this place.

Collapse
 
itsjp profile image
Jaydeep Pipaliya • Edited
return patternNames.reduce((acc, name) => {
    if (patterns[name]) {
        acc[name] = patterns[name];
    } else {
        acc[name] = "Description not available for this pattern.";
    }
    return acc;
}, {});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
efpage profile image
Eckehard

What about this?

return  {[patternName] : patterns[patternName]??"No description available"}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aminnairi profile image
Amin • Edited

Similar solution to Jaydeep Pipaliya, written in TypeScript.

const getCorrectDescription = (searchedPatternName: string) => {
  const 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 Object.entries(patterns).reduce((matchingPatterns, [patternName, patternDescription]) => {
    if (patternName === searchedPatternName) {
      return {
        ...matchingPatterns,
        [patternName]: patternDescription
      }
    }

    return matchingPatterns;
  }, {});
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
stio profile image
stio • Edited

return patterns?.[patternName]

Collapse
 
zeeh1975 profile image
zeeh1975

...
return patterns.hasOwnProperty(patternName)
? { [patternName]: patterns[patternName] }
: null;
...

Collapse
 
web3space profile image
Jasper

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