Hello Frank,
Getting frustrated is the life of a programmer. I program all the time and I still find moments where I want to beat my head against a wall. Sometimes is best to just walk away for a little bit and then come back.
As for your questions, let me try to clarify some things for you.
Arrow functions and ternary statements are two completely different things. Let me explain:
function addThree(num){
return num + 3;
}
const addThree = (num) => num + 3;
const addThree = (num) => { num + 3; };
All three of these above are the same thing.
[2, 4, 6, 8].filter(function(num){
return num > 5;
});
[2, 4, 6, 8].filter((num => num > 5));
Both of these above are the same thing.
if (a > b){
answer = "a is greater than b";
}
else {
answer = "b is greater than a";
}
answer = (a > b) ? "a is greater than b" : "b is greater than a";
both of the above are the same thing.
Now to explain what map / filter is.
you may have seen
var arr = [2, 4.4, 5, 1.2, -3];
arr.filter((num) => Number.isInteger(num));
Let’s simplify this to make it easier to read.
[2, 4.4, 5, 1.2, -3].filter((num) => Number.isInteger(num));
All i did was replaced arr with the actual array.
.filter() returns a new array, and the values that go into that new array must result to true. If that value is false, then it is ignored.
So with: [2, 4.4, 5, 1.2, -3].filter((num) => Number.isInteger(num));
the first thing that happens is
num = 2; (first element in array). then num = 4.4 (second element in array) then num = 5 and so on… until all the elements have been used once.
the => functions as described before would look like this with es5
[2, 4.4, 5, 1.2, -3].filter(function(num){ return Number.isInteger(num); });
So what is happening here is filter is returning Number.isInteger(2); // on first iteration. Which is true.
since it is true that number is returned into a new array.
With the 2nd number (4.4) the return of Number.isInteger(4.4) is false, so that value is ignored.
so with var integerOnly = [2, 4.4, 5, 1.2, -3].filter((num) => Number.isInteger(num));
// integerOnly now becomes [2, 5, -3];
Map works similar to filter,. except that it doesn’t care if the return value is true or false. It just builds a new array based on what you return.
var plusOne = [1, 3, 5, 7].map((num) => num + 1));
this returns a new array with values [2, 4, 6, 8];
Hope this helps!