Answer the question
In order to leave comments, you need to log in
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=== 'vegan') {
offerVegan();
} else {
offerMeat();
}
Answer the question
In order to leave comments, you need to log in
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();
}
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 questionAsk a Question
731 491 924 answers to any question