0

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.

2
  • Where is the name property coming from? Commented Oct 27, 2021 at 12:02
  • 1
    If you need to call functions by evaluating the function name at runtime, you've probably made some severe gaffes in design. I would implore you to take several steps back and understand the requirement itself before you end up with a codebase that is completely unmaintainable long-term (what happens if you end up having to support several hundred, several thousand, or even several million of these heros values? Do you anticipate actually going in and creating a single function for each of the innumerable heros you'll have?) Commented Oct 27, 2021 at 12:25

4 Answers 4

1

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'));

Sign up to request clarification or add additional context in comments.

Comments

0

You can call the functions and compare the returned string:

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().includes(param)) : false;

console.log(myFunc('StarFire'));

Comments

0

You can do it with the help of Map. You should place heroStringsArr as key and your functions as value:

const map = new Map();
map.set('Robin', () => console.log('Hey Robin'));
map.set('Raven', () => console.log('Hey Raven'));
map.set('StarFire', () => console.log('Hey StarFire'));
map.set('BeastBoy', () => console.log('Hey BeastBoy'));

const myFunc = param => map.has(param) ? map.get(param) : console.log('This function is not available!');

myFunc('Robin')();

Comments

0

I like @Andy's idea of just one function. Also if you must have many functions, essentially multiple "instances" of the same function, you can start of with an array of strings which you can then map using the one function to an array of functions.

You can as well use regular expressions as in the demo below:

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 => herosFuncArr.filter(f => (new RegExp(`\\b${param}\\b`)).test( f() )).length > 0; 
console.log( myFunc('StarFire') );

Comments

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.