1

I'm playing around with some functions and I noticed that these two functions seem to have different outputs in a pure function. Am I right in thinking that these two outputs are different?

counter = 0;
function createObject1(name, calories, fat, carbs, protein) {
    counter += 1;
    return { id: counter, name, calories, fat, carbs, protein };
}

function createObject2(...args) {
    counter += 1;
    var data = {};
    data['id'] = counter;
    var headers = args[args.length - 1];
    for (var i = 0; i < args.length-1; i++) {
        data[headers[i]] = args[i]
    }
    return data   
}

// How I'm calling them
createObject1('Cupcake', 305, 3.7, 67, 4.3);
createObject2('Cupcake', 305, 3.7, 67, 4.3, ["name","calories","fat","carbs","protein"]);

Thanks for your help! :-)

5
  • They are totally different: the latter expects something called "headers" as the last argument. Commented Sep 13, 2018 at 23:19
  • 1
    Yes different, the second one you will call it like this createObject2(1,2,3,4,5, ['name', 'calories', 'fat', 'carbs', 'protein']); Commented Sep 13, 2018 at 23:23
  • Sorry, I must have phrased my question weird. I'm wondering if the return outputs are different? Commented Sep 13, 2018 at 23:25
  • 2
    @Tim I believe the output would be the same for both Commented Sep 13, 2018 at 23:26
  • @AlleoIndong Thanks, my bug must be something else then. I appreciate the help Commented Sep 13, 2018 at 23:27

2 Answers 2

2

Well, let's try it out:

counter = 0;
function createObject1(name, calories, fat, carbs, protein) {
    counter += 1;
    return { id: counter, name, calories, fat, carbs, protein };
}

function createObject2(...args) {
    counter += 1;
    var data = {};
    data['id'] = counter;
    var headers = args[args.length - 1];
    for (var i = 0; i < args.length-1; i++) {
        data[headers[i]] = args[i]
    }
    return data   
}

console.log(JSON.stringify(createObject1('Cupcake', 305, 3.7, 67, 4.3)));
console.log(JSON.stringify(createObject2('Cupcake', 305, 3.7, 67, 4.3, ["name","calories","fat","carbs","protein"])));

Output is:

{"id":1,"name":"Cupcake","calories":305,"fat":3.7,"carbs":67,"protein":4.3}
{"id":2,"name":"Cupcake","calories":305,"fat":3.7,"carbs":67,"protein":4.3}

If by pure function you mean the concept used in functional programming (a function that always returns the same output for a given input) then these functions are not pure. A pure function must only depend on it's parameters as input, therefore the global variable counter breaks this rule.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are missing the point of the pure functions.

A function is only pure if, given the same input, it will always produce the same output

You functions createObject1 and createObject2 does the Same job in different ways and are expected to give same results on the if same input is provided, So cannot say there functions are different based on their functionalities.

But as these functions are Not pure functions because it has a state counter which is maintained in globally.

Hence for same inputs, you can never receive same outputs here, as your output is dependent on ever changing global state counter

I hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.