@gregorgrinc (GregorGrinc) posted a new topic, subject "How do I know if I am picking values from right object?". It read:

   **How do I know if I am picking values from right object? **  
    At the end I solved the problem that I had with this part of a course but I have one additional question. Let's suppose I had two objects that are constants and bouth of them have the same parameters like “max” and “min”. How would I declare from which objects I want values that represent the parameters of a object?
    
    I wanted to by more specific and at first I had done this but I was getting errors:
    [code] 
        const half = ({max, min} = stats) => (max + min) / 2.0; 
        
    [/code]
    [code] 
          **This is the correct solution to the problem**
        
    [/code]
    [code] 
        const stats = {
        max: 56.78,
        standard_deviation: 4.34,
        median: 34.54,
        mode: 23.87,
        min: -0.75,
        average: 35.85
        };
        
        // Only change code below this line
        const half = ({max, min}) => (max + min) / 2.0; 
        // Only change code above this line
        
    [/code]
    [code] 
          **Your browser information:**
        
    [/code]
    
    User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36`
    
    **Challenge:** Use Destructuring Assignment to Pass an Object as a Function's Parameters
    
    **Link to the challenge:**
    
    freecodecamp.org
    
    ### freeCodeCamp.org
    
    Learn to Code — For Free

@jsdisco replied

    In the lower left in the test panel of the challenge, you can see that the function `half` is called with the object `stats`:
    
    `half(stats) should be 28.015`
    
    That's where you specify which object you'd like your function to use. You could call it with a different object:
    [code] 
        const otherStats = {
          max: 100,
          min:1
        };
        
        half(otherStats)
    [/code]

@gregorgrinc (GregorGrinc) replied

    Ou, so when I call function half I just put there some object that has two parameters max and min and that is it? Function will find those arguments in object by itself if I get it right?

@jsdisco replied

    Almost. You put _one_ parameter into it (some object), which has two _properties_ (max and min). The function will know that it receives one argument, because of your function declaration:
    [code] 
        // This is the argument being passed in, and you destructure the object right away
        // to extract the two properties max and min from it
                          |
                          |
                          V
        const half = ({max, min}) => (max + min) / 2.0; 
        
    [/code]
    
    This does exactly the same:
    [code] 
        const half = (stats) => (stats.max + stats.min) / 2.0; 
        
    [/code]

After which, @system (system) added

    This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.


{'source': 'discourse', 'url': 'https://forum.freecodecamp.org/t/how-do-i-know-if-i-am-picking-values-from-right-object/463994', 'title': 'How do I know if I am picking values from right object?', 'views': 135, 'has_accepted_answer': True, 'word_count': 412, 'like_count': 2, 'reply_count': 2, 'participant_count': 2, 'posts': 5, 'tags': '', 'author': '@gregorgrinc (GregorGrinc)', 'participants': '@gregorgrinc (GregorGrinc), @jsdisco, @system (system)', 'image_url': [None], 'created_at': '2021-06-08T09:23:55.060Z', 'last_posted_at': '2021-12-08T02:04:07.640Z'}