0

I am trying to create a 'secret' guestList to practice some closures and binding in JS. I am currently stuck because I need to use binding so the value of i updates after every iteration but I am really new to biding and I am having trouble wrapping my head around this... how do I call my variable code from the closure? how do I correctly bind the guesName to my checkCode function? :/

Here my code :

function guestListFns(guestList, secretCode){
    var topSecretList = []; 
    function codeChecker(code) {
        if (code === secretCode) {
            return guestName;
        } else {
            return "Secret-Code: Invalid";
        }
    };
    for(var i = 0 ; i < guestList.length; i += 1){
        var guestName = guestList[i]; 
            topSecretList.push(codeChecker.call(this.guestName, code));
    }
    console.log(topSecretList);

        return topSecretList; 
}

my testing values :

    var guestListFns = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512);

var guest = guestListFns[1](512);
console.log(guest);

my return value so far:

"code is not defined" 

Also, I have already figured out how to implement this function simply using map. But what I meant with this exercise is to practice binding so I can understand the concept.

Thanks!

2
  • it is completely unclear whatyou actually want as you are mixing up a few things Commented Mar 16, 2018 at 22:44
  • I would love to understand what is the correct way to bind my checkCode function to each guestName and assign that checkCode function to the my topSecretList arr... Commented Mar 16, 2018 at 22:47

3 Answers 3

1

You dont wanna call, but you want to bind:

codeChecker.bind({ guestName })

So now inside codeChecker you can access

this.guestName

Actually you are overcomplicating things:

 const guestListFns = (arr, secret) =>
   arr.map(el => code => code === secret ? el : "nope");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Jonas . I actually had figured out already how to use map. But, as stated in the question, my idea is to understand binding. In your answer to my question : when you use the {} around guestName, should I use them too?? After using bind instead of call and using this.guestName inside my checkCode function, i get undefined : /
1

If I understand what you are trying, I think call() is the wrong way to go about this. You want an array of partial functions where each function already has the name. You can use bind() for this. call() actually invokes the function, which isn't what you really want here. bind() returns a new function and allows you to set this and/or some of the arguments:

function guestListFns(guestList, secretCode){
    var topSecretList = []; 
    function codeChecker(guestName, code) {
        if (code === secretCode) {
            return guestName;
        } else {
            return "Secret-Code: Invalid";
        }
    };
    for(var i = 0 ; i < guestList.length; i += 1){
        var guestName = guestList[i]; 
        topSecretList.push(codeChecker.bind(null, guestName));
    }

    return topSecretList; 
}

var guestListFns = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512);

console.log(guestListFns[1](512)); // ben
console.log(guestListFns[2](512)); // dan
console.log(guestListFns[1](112)); // bad code

1 Comment

Thanks @Mark, as I in the previews answer, after modifying my code following your answer, my output is [ null, null, null, null, null, null, null ] 'Gabriel' 'Ben' 'Dan' 'bad code I wonder why isn't .push passing the function into topSecretList instead of null :/
0

Might this be what you're looking for?

function guestListFns(guestList, secretCode){
    var topSecretList = []; 
    function codeChecker(code) {
        if (code === secretCode) {
            return this.guestName;
        } else {
            return "Secret-Code: Invalid";
        }
    };
    for(var i = 0 ; i < guestList.length; i += 1){
        var guestName = guestList[i]; 
        topSecretList.push(codeChecker.bind({guestName:guestName}));
    }
    console.log(topSecretList);

        return topSecretList; 
}

var gfn = guestListFns(["Gabriel", "Ben", "Dan", "Griffin", "Cang", "Kate", "Chris"], 512);
gfn.forEach(f => console.log(f(512)));

5 Comments

Thanks @TKol , yes. This solution apparently solves the binding of checkCode inside my loop. Now, any idea of why my topSecretList is filled with null instead of the function checkCode itself? For some reason I can't get the push to pass the function instead of its return value... some guidance is fine. It is just a really confusing topic.
when I run mine, it's not filled with null.
Weird. In my case it returns [ null, null, null, null, null, null, null ] 'Gabriel' 'Ben' 'Dan' 'Griffin' 'Cang' 'Kate' 'Chris' :/
you should try console.logging the typeof the contents of topSecretList, topSecretList.forEach(f => console.log(typeof f))
Right! they appear to be functions... thank you so much! I am still a bit confused as for why the null in my array but it seems like internally is working.

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.