Javascript Sections Quite Hard

Sooo, it took me a while to even find out how to post here. I accidently posted to a comment from 21 days ago:-(

I have been doing well with the web development path.

Am I the only one who is having a hard time with javascript–from the whale project to higher order functions?

The language in the explainations is very confusing and not helpful. The code itself in the higher level section was not clear and helpful.

I feel quite disappointed and I hope the rest of the course does not take such a dive in quality.

just to add some more: Here is the entire solution. I feel like so much is disconnected. What is the “problem” we are trying to solve here? What is funcParameter here, a function? … why have addTwo–what is the argument for num? Maybe I suck at this and didnt learn enough from the previous lessons, but below is really confusing.

const checkThatTwoPlusTwoEqualsFourAMillionTimes = () => {
for(let i = 1; i <= 1000000; i++) {
if ( (2 + 2) != 4) {
console.log('Something has gone very wrong :frowning: ');
}
}
};

const addTwo = num => num + 2;

const timeFuncRuntime = funcParameter => {
let t1 = Date.now();
funcParameter();
let t2 = Date.now();
return t2 - t1;
};

// Write your code below

const time2p2 = timeFuncRuntime(checkThatTwoPlusTwoEqualsFourAMillionTimes);

const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry
} else {
return ‘This function returned inconsistent results’
}
};

checkConsistentOutput(addTwo, 10);

1 Like

That lesson tries to teach us that functions can be reapplied to variables for better reading and that functions can be used as parameters, too.

Doing that can create antipatterns that can lead down into deep rabbit holes, which can be bad. But can also be very useful in cases in which you know what your code does exactly. Documentation is the key here, this is kind of missing in this lesson, I can imagine it wasn’t emphasised because it could make this more confusing than it already is.

Trying to explain what num is and where it is used:

checkConsistentOutput(addTwo, 10);

Invoking this function uses the addTwo function for a parameter, addTwo itself needs a parameter, too. I believe you’re looking for where addTwo gets a param passed to itself?

Here:

const checkConsistentOutput = (func, val) => {
let firstTry = func(val);
let secondTry = func(val);
if (firstTry === secondTry) {
return firstTry
} else {
return ‘This function returned inconsistent results’
}
};

second line of this function: let firstTry = func(val);
and third line: let secondTry = func(val);

Basically you could read that as

const checkConsistentOutput = (addTwo, 10) => {
let firstTry = addTwo(10);
let secondTry = addTwo(10);
...

The firstTry and secondTry variables store whatever is the outcome of invoking the function that was passed as func with the val for a param that was passed for a second param to the function checkConsistentOutput.

Hope this makes sense, this multi-layered thing is quite difficult to understand at first, but once the new axis created that way “clicks” it will add a whole new dimension to the whole coding idea.
Or, worst case, break your codebase if applied maliciously…

1 Like