The code below tries to print "even" for numbers divisible by 2.
If it is not (test) then (), but not: if (! Test) then () when the checked state is "n% 2". In the code below, it seems that “IF numbers are NOT divisible by 2, they print out“ number equals ”, which does not seem logical.
More generally, what are the advantages of writing an Unless function over using an If statement to specify a condition where we can simply write if (! Condition)?
Any help is greatly appreciated.
function unless(test, then) { if (!test) then(); } function repeat(times, body) { for (var i = 0; i < times; i++) body(i); } repeat(5, function(n) { unless(n % 2, function() { console.log(n, "is even"); });
javascript function oop if-statement conditional-statements
Henry
source share