DEV Community

Cover image for JavaScript Challenge: What would be your solution to this challenge? 🥷🏻🧩

JavaScript Challenge: What would be your solution to this challenge? 🥷🏻🧩

Renan Ferro on April 11, 2024

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

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