timoxley/functional-javascript-workshop

Exercise 10 (PA with Bind) - multiple solutions pass the test

swinston100 opened this issue · 2 comments

In my below solution all the return statement will result in the test passing. Perhaps I do not understand bind (and if that is the case I'm sorry please help me) but I would not expect this behavior.

module.exports = function(namespace) {

    //create arrray of strings from the arguments
    var args = Array.prototype.slice.call(arguments);

    //all of these work!!!!
    return console.log.bind(null, args.join(' '));
    return console.log.bind(null, args[0]);
    return console.log.bind(null, namespace);

};

Strangely the solution to exercise 9 also works.

Hey, @swinston100! It's been a month. Have you figured this out?

The reason that these all pass is not because of the way bind works, but it's because args.join(' '), args[0], and namespace all equal the same thing. You're using different techniques to pass in the same argument to bind.

If 'hero'is passed in as namespace to the original function, then:

  • namespace obviously is 'hero'
  • args[0] is 'hero', because 'hero' is the first argument passed into the function (make sure you understand what Array.prototype.slice.call(arguments) is doing)
  • args.join(' ') is 'hero', because there is not more than one item in your args array to join together with ' ' — so it's just 'hero'

SO, if you swap the references with the value they represent all your return statements are return console.log.bind(null, 'hero'); if 'hero' were the namespace passed in.

I haven't looked at this prompt in a while, but, in my opinion, the namespace one is best. It allows you to delete the extra args line that you have.

Many thanks, @ethangodt! I did figure it out and then forgot to close it, sorry and thanks for confirming my understanding! :-)