A
A
Artur9912020-12-16 21:16:23
JavaScript
Artur991, 2020-12-16 21:16:23

How to check if a callback function parameter is a specific string?

There is a function with three callback functions

function offerMeal(
  clientPreferences,
  offerVegan,
  offerMeat,
) {


If clientPreferences returns the string 'vegan', return the call to offerVegan, otherwise return the call to offerMeat.

I'm writing through if/else, and I can't figure out how to specify a condition that if 'vegan' gets into the parameter, offerVegan should be called.

Here's what's worked out so far:
if (clientPreferences=== 'vegan') {
    offerVegan();
  } else {
    offerMeat();
    }


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dexdot, 2020-12-16
@Artur991

In order to get the return value from a function, that function must be called, which you forgot to do.
Corrected code:

const preferences = clientPreferences();
if (preferences === 'vegan') {
  offerVegan();
} else {
  offerMeat();
}

N
Nadim Zakirov, 2020-12-16
@zkrvndm

Where is the guarantee that clientPreferences contains a string, and not some object or function? There are no such guarantees, so it makes sense to first check the type of a variable with typeof, and only if it is a string to check for a match with the word vegan.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question