Calrifications for declineEverything() please

Below is an image of my original code and the output.
The output seems to me to be what the challenge asks for but the editor is not accepting it.

I suspect my problem is not understanding what the below quote actually means.

The .forEach() function should apply politelyDecline() directly; it should NOT merely receive an argument function that uses politelyDecline() .

I do not understand the differenct between

forEach() should apply politelyDecline() directly

and

it should NOT merely receive an argument function
Any advice ??

Not sure either, but this is how I understand it:

const arr = ['what', 'ever', 'item'];
const fn = x => { console.log(x)}

The .forEach() function should apply politelyDecline() directly;

arr.forEach(fn);

it should NOT merely receive an argument function that uses politelyDecline()

arr.forEach(el => fn(el))

@mirja_t is right, however, I would like to clarify. the .forEach accepts a function. We could generate a function on the fly:

arr.forEach((element) => { ... } )

but we can also reference an existing function:

arr.forEach(politelyDecline);

.forEach now takes care of calling the function and providing the argument needed for your veg parameter

its quite a bit of abstractions which you need to see through

1 Like

Thank you both @stetim94 @mirja_t for swift reply.
Below is another screenshot - I think I followed @mirja_t advice correctly
However it still generates error.
Am I just missing something so obvious I can’t see it?

you invoke/call the politelyDecline function, this you shouldn’t do. You should pass the function by reference, so that the forEach method can invoke the function for you (for each element)

Ahh -
so - politelyDecline rather than politelyDecline()

thank you both @stetim94 and @mirja_t

1 Like

I would try to pass 2 parameters into declineEverything: array and funcToApply (politelyDecline). Then in declineEverything use forEach on the array and apply the function to each element.

yes, but being able to understand the impact/significance of this change is most important

thank you for your comment.
It seems to work with only passing one parameter - an array
then the decline everything will do the .forEach the application of politelyDecline

it seems to work.

1 Like

I am not sure about the full improtance of this.
But I understand that politelyDecline is a variable which has a function as its value.

and that the way I’ve done it before politelyDecline() is actually calling a function - but there is no politelyDeline function only politelyDecline variable

mike7127143742 suggestions was next level and outside of the scope of this exercise, this means we can choice which callback function we want to apply. So for example:

function declineEverything (array, callbackFunc) {
   arr.forEach(callbackFunc);
}

declineEverything(veggies, rudeDecline);

This kind of abstraction would enable us to switch easily between different kind of decliment. Although very interesting, maybe a bit too much at the moment

2 Likes

Also keep in mind that politelyDecline() does not make sense as politelyDecline takes 1 parameter. So when you invoke this function - it needs one parameter such as politelyDecline(foodItem). And the forEach needs a callback function with one parameter as well (the current element of the array).

So is this why it worked with element => politelyDecline(element)
at that time it had something to operate on. ??

yes, but now you have an arrow function (element =>) which only purpose in life is to call another function. So you have a wrapper function which is only bloating your code

Using both (a one-off function and existing function) is not the way to go

At which time we would get
f*** off with your veggies[i]! Give me pizza now`
:slight_smile: ?

you would still need to make the rudeDecline function, but yes :slight_smile:

1 Like

I see - lean code is better.

Thank for everyone in this short impromptu discussion.
Really helped me.
I usually am doing this all on my own which can be challenging…

is there a way to download this conversation?

unfortunately not, if only live was that simple. Its always a balance, if your code becomes so lean that is really difficult to read the code (to understand what the code does), its not good

but writing too much abstractions/layers/bloating is also not good

the most important thing is being able to weigh trade-offs, a skill which take a while to learn.

2 Likes