I’m a bit confused by this lesson. I didn’t see any results in the console, so I wasn’t sure exactly what the code was doing, or trying to do. When I tried to print the variables to the console log, I received an error message.
Also, In the last exercise, when I typed "checkConsistentOutput(addTwo, 2);’ I got an error - but the solution was checkConsistentOutput(addTwo, 10);. What did this check, and why did 10 work and not 2? Why wouldn’t it let me print the answer to the console?
This is what I had for the last question and it worked, but still I’m a bit confused over that whole lesson. Might take me a bit to really understand higher-order functions. Especially the anonymous arguments part…
function checkConsistentOutput(func, value) {
let first = func(value);
let second = func(value);
if (first === second) {
return first;
} else {
return “This function returned inconsistent results”;
}
}
console.log(checkConsistentOutput(addTwo, 5));
As I understand it, the following code actually invokes the function ‘checkConsistentOutput()’ and passes in the callback function ‘addTwo()’ as well as a value (in this case 20, though I experimented with many values and they all worked):
checkConsistentOutput(addTwo, 20);
Inside the ‘function body’ of ‘checkConsistentOutput()’, the ‘addTwo()’ function is invoked twice (in the first two lines of the 'function body) and passed the value (in this case, 20):
const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry;
} else {
return 'This function returned inconsistent results';
}
};
That passed the value 20 into the ‘addTwo’ function, performing ‘num + 2’ (in my case 20 + 2… resulting in 22):
const addTwo = num => num + 2;
The return value of 22 was passed back to checkConsistentOutput(), compared in firstTry and secondTry, found to be equal (’===’) and returned. I logged it to the console to see the result in this manner:
Abstraction allows us to write complicated code in a way that’s easy to reuse, debug, and understand for human readers. Can anyone explain me what is abstraction in programming?