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

Renan Ferro @renancferro

About: Frontend Developer ❤️‍🔥 | Just learning, practicing, coding and letting a little bit of it spread out forever and ever ➿ Be brave enough to be bad at something new!

Location:
Brazil
Joined:
Apr 20, 2021

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

Publish Date: Apr 11 '24
9 18

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

Comments 18 total

  • Eckehard
    EckehardApr 11, 2024
    return  {[patternName] : patterns[patternName]}
    
    Enter fullscreen mode Exit fullscreen mode

    See Computed Property Names

  • wangzhi
    wangzhiApr 12, 2024
    if (patterns.hasOwnProperty(patternName)) {
      return {[patternName] : patterns[patternName]}
    } 
    
    Enter fullscreen mode Exit fullscreen mode
    • Eckehard
      EckehardApr 12, 2024

      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.

      • wangzhi
        wangzhiApr 14, 2024

        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
        • Eckehard
          EckehardApr 14, 2024

          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.

          • wangzhi
            wangzhiApr 14, 2024

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

            • Eckehard
              EckehardApr 14, 2024

              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?

              • wangzhi
                wangzhiApr 14, 2024

                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

                • wangzhi
                  wangzhiApr 14, 2024

                  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

                  • Eckehard
                    EckehardApr 14, 2024

                    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.

                    • Eckehard
                      EckehardApr 14, 2024

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

  • Ben Sinclair
    Ben SinclairApr 12, 2024

    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 ({})?
  • Jaydeep Pipaliya
    Jaydeep PipaliyaApr 12, 2024
    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
    • Eckehard
      EckehardApr 12, 2024

      What about this?

      return  {[patternName] : patterns[patternName]??"No description available"}
      
      Enter fullscreen mode Exit fullscreen mode
  • zeeh1975
    zeeh1975Apr 12, 2024

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

  • Amin
    AminApr 12, 2024

    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
  • Jasper
    JasperApr 12, 2024

    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

Add comment