Where do I Belong . a - b in sort is undefined?

Tell us what’s happening:

Hi Guys,

I have a few questions relating to this question. I have put //comments within the code. OK so we need to get the sort function counting, how do we define a and b here?

On top you can see my original thinking, trying to take an inefficient shortcut below is taken from another answer from a help topic someone posted. They both run into the same definition problem for me

All help much appreciated

Muchos Thankos

Your code so far


function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  
var arr2 = [...arr]

var insertInArray = arr2.splice(0,0, num);

arr2.sort(() => a - b) // How do we define a and b?

// Same for this below, how do we define x and y?
//arr.sort( function(x, y)
//{
//return x - y;
//} 

// Can we use arr[i] - arr[i++]?

return arr2.indexOf(num)

//Now realise method above will run into trouble if there is more than one of the same number no? and its not very efficient.
//arr.sort(() => a - b)
  
// for (var i=0; i<arr.length; i++) {
//   if (arr[i]>=num) {
//      return i;
//   }
//  }
//}



  

  

  









}

getIndexToIns([40, 60], 50);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/where-do-i-belong

arr2.sort(() => a - b)

The variables those two variables are parameters passed in by the sort function. Different methods pass in different variables, some of which are optional. I often have to consult the docs if it’s a method I don’t use much or need to use a parameter I often have to consult the docs. The docs for sort are here. The first two parameters sent are the two things you want to compare - it is common to call them a and b with sort - but you could call them anything. You need to “receive” those variables inside the () at the beginning of your callback as (a, b). That would give you:

arr2.sort((a, b) => a - b)

or in ES5 it would be:

arr2.sort(function(a, b) { return  a - b })

The (a, b) tells the callback what you want to call those, and the a - b tells the callback how to process them to return a value.

Yes, callback functions are very confusing. But they are very powerful and you’ll get it if you keep at it.

1 Like

Can we use arr[i] - arr[i++]?

Without looking at your code, the problem is that i and i++ will both give the same value. The i++ operator increments the variable after it is evaluated, so you would be subtracting arr[i] from itself. I think what you wanted is ++i.

But that would not work with the sort method because of how it works. But in as different context, with a different objective, that might be useful.

// Same for this below, how do we define x and y?

arr.sort( function(x, y)
{
  return x - y;
} 

That should work. You are telling the function what you want to call the parameters that you are receiving from sort.