Function within a function

One thing to note, often callback functions are written as arrow functions. Arrow functions are just another way to write a function.

// Traditional syntax
function add1(a, b) {
  return a + b;
}

// Arrow syntax
const add2 = (a, b) => a + b;

// Other arrow syntax
const add3 = (a, b) => { return a + b; };

These two three are both all functions that do the exact same thing.


Also, the name ‘callback function’ isn’t special or anything. It’s just what we usually say instead of ‘function passed to another function as an argument’.

Yeah, I guess that could have inserted that inbetween steps 3 and 4 in my escalation.

Array.prototype.myMap3a = function(callback) {
  var newArray = [];
  for(let i = 0; i < this.length; i++) {
    newArray.push(callback(this[i]));
  }
  return newArray;
};


var new_s3a = s.myMap3a(function doubleIt4(item) {
  return item * 2;
});

console.log(new_s3a);

Yeah I definitely tried to apply myself throughout all th sections and after first struggling with the beginner alg section I redid everything, then got to int alg and now redid the last few sections but not sure beyond that what I could do.

This one was easier to get down for me the first time going through, though the endless new concepts etc adding on top of each other may overload my mental cognition lol

Yeah so callbacks are themselves able to hold arguments and the function in the callback can have a argument basically right?

That’s sort of how programming works, though JavaScript is pretty notorious for having a wide variety of old school and new school syntax to express a wide range of ideas.

1 Like

Yeah I remember when I first saw the ES6 section I was like can I just focus on one? lol

Yes. Callbacks are functions. You can do anything to it that you can do to a function. If you pass in a function, the callback is pointing to the exact same function (it’s not a new function, it’s still the old one, just with a different label). They are two labels that point to the same section of code in memory that is the code for that function.

2 Likes

the endless new concepts etc adding on top of each other

If this were easy, it would be done by high school dropouts for minimum wage. This stuff is tough. Cut yourself some slack. Every good coder has struggled with something. For most of us mere mortals, that happens on a regular basis.

You’ll always struggle with something. As you get better, it will just be with more complex things. Just keep at it.

2 Likes

I can relate to your frustration because I remember how I felt when I hit that same wall. The thing is - it doesn’t matter how often or how well someone explains it to you. You won’t get past that wall only by understanding this one challenge.

I’d recommend to pick a coding challenge site (edabit and codewars are good), and go for very easy challenges. The point of the exercise is not to solve a difficult coding problem, the point is to build up muscle memory for how JS coding works in general.

Solve them once with the tools you’re most comfortable with (for loops and if/else). Then redo them using Array methods (.map, .filter, .reduce). Then redo them again, using your own custom helper functions. Look at other people’s solutions, look at with how little code the challenge can be done.

Dedicate one hour/day to that for a week or two, and enjoy watching yourself getting a better understanding of how it all works together.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.