Miss understand some part of Higher-order functions

I understand how this function works. But I do not understand where // → 2 is even comes? It calls by repeat function together with unless ?
Thank you for answer.

I’m not sure if im qualified enough, but:

  1. Repeat is just a function that takes two arguments: a number(n), and a function(body). The repeat's sole purpose in life is to call body function n times and passing a current number as body's argument. Here, you are calling repeat(3, body(n)) which basically equals this:
    body(0);
    body(1);
    body(2);

  2. Unless is a function that takes some statement (test) and a function (then). Unless calls then function if the test's result is a falsy value. So, what you have at lines 2782-2785 can be translated into:
    unless(0 % 2, then); // 0 % 2 == 0
    unless(1 % 2, then); // 1 % 2 == 1
    unless(2 % 2, then); // 2 % 2 == 0

So your then() function will be called only for the first and third call of unless, printing whatever you have passed into console.log.

I know it’s not exactly the clearest explanation, but I hope it helps.

2 Likes

Yes,it helps)) Thanks

1 Like

Glad I could help :D.

I’ve never thought that eric cartman will help me to explain high funct)

1 Like

His dickish attitude is just a pose, hes really sensitive and helpful on the inside :stuck_out_tongue:

1 Like

I forgot to ask the question. In this case 0 == false? Or it’s always so?

0 is considered a falsy value by JavaScript. Falsy value is a value that, after converting to boolean, is false. Falsy values in JS are:
false
0
NaN
null
undefined
"" //empty string

So 0 is not technically false but the snippet you provided uses if statement. In JS, the condition you provide for if is implicitly coerced (“coerce” mean more or less “convert”) to boolean. So the whole 0 % 2 statement is later coerced to boolean by JS behind the covers, and that resolves to false.
Again, not the cleanest explanation, sorry for that…

On the side note, when you use == operator (loose equality) you allow JS to implicitly coerce values while comparing them. So if you actually type 0 == false in the console, it will print true. To compare values with coercion disallowed, you use ===.

I understand.Thank u.
You have a polish surname. Are you polish?)

Cartman is in love now! No wonder he’s being helpful, Butters;)

Anyway, I wrote an extensive answer on higher order function (although for different question). Hope this might help you get yourself clarified.

2 Likes

@SerafimPoch Yup, I’m from Warsaw more specifically :slight_smile:

@adityaparab And girls can be totally funny dude. Good link there, very detailed answer!

@SerafimPoch
Here is a great resource comparing all the funky JS truth-false values: http://dorey.github.io/JavaScript-Equality-Table/

1 Like

I’m from Ukraine. Live in Kiev. I want go to Krakov or Warshava for excursion . Watch some architecture etc. What do you think, I should go to Krakow or Warshava ?)