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.

Answer

You’re far better off eliminating all of those functions, and creating one callHero function into which you can pass the name of the found hero and return a string. You don’t need to worry about filter; use find to find the first match.

And it’s best not to use includes because that will match Star to StarFire which you probably don’t want to do. Just do a simple comparison instead.

const heroes = ['Robin', 'Raven', 'StarFire', 'BeastBoy'];

// Return a string
function callHero(hero) {
  return `Hey ${hero}!`;
}

function isAHero(name) {

  // Find the hero in the array
  const hero = heroes.find(hero => hero === name);

  // If it exists call the `callHero` function with the hero name
  // and return the resulting string from the function
  if (hero) return callHero(hero);

  // Otherwise return something else
  return `Boo! ${name} is not a hero.`;
}

console.log(isAHero('Robin'));
console.log(isAHero('Billy Joel'));
console.log(isAHero('StarFire'));
console.log(isAHero('Star'));