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!