mercredi 27 octobre 2021

How to filter an array that contains functions

so like I'm required to write if/else statement in shorthand or in functions instead of :
if (hero === "Robin"){return callRobin()}... or instead of switch cases.

const callRobin = () => `Hey Robin`;
const callRaven = () => `Hey Raven`;
const callStarFire = () => `Hey StarFire`;
const callBeastBoy = () => `Hey BeastBoy`;

// these were the functions!!

const herosFuncArr = [callRobin, callRaven, callStarFire, callBeastBoy];  //an array that contains the functions
const herosStringsArr = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];
const myFunc = param => param == herosStringsArr.filter(x => x.includes(param)) ? herosFuncArr.filter(z => z.name.includes(param)()) : false;
myFunc('StarFire');

my point in this code was : when we inter a hero name as a parameter, if it exists in the strings array, return an element from the functions array that has the same letters the parameter have AS A FUNCTION as indicated with the double parentheses.

I tried so many things, also tried eval (`call${param}()) but apparently that's unacceptable. also tried .toString but didn't work (for me). any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire