Understanding arguments being passed into functions, inside of other functions

Tell us what’s happening:
Hello all! I have a question regarding the process of calling a function, inside of a function. Where I get confused is what arguments get passed in. Below I have the resolution code for the problem, “Where do I Belong”, where they want you to return the index value of the array, where “num” would fit. The code makes sense for the most part; however, I get a bit lost when I try to imagine what is being passed in as “a” and “b” in the code: arr.sort(function(a,b){return a-b;}); Would “arr” and “num” be the passed arguments into the function(a,b)? Several problems have been throwing me off, the deeper I get into “function-ception”. Any feedback/guidance to better under this concept is much appreciated. :slight_smile:

Your code so far


function getIndexToIns(arr, num) {
 arr.sort(function(a,b){return a-b;});
 for (let i = 0; i < arr.length; i++){
   if (arr[i] >= num)
   return parseInt(i);
 }
 return arr.length;
}

console.log(JSON.stringify(getIndexToIns([10, 20, 30, 40, 50], 35)));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Link to the challenge:

The function you’re passing to sort (known as a “callback”) will get called by sort with two arguments, which consist of two items in the array to compare. The function should return a negative number if item a is to come before item b, a positive number if b comes before a, and zero if they’re equal. The documentation for sort explains this in more detail with examples:

2 Likes

Thank you for clarifying! It’ll become even clearer the more I practice them. Thanks!

If you want to see something that I would consider real “function-ception”, research currying

Example:

const sum = function(x) {
   return function(y) {
      return function(z) {
         return x+y+z;
      }
   }
}
1 Like

Haha, this confused me when I read it originally. I’ve now entered the section that introduces it. Quite an interesting concept!