 # Explanation of hint 1 and hint 2

Tell us what’s happening:
I have worked on this challenge so far, and I have sorted the array with sort()
However, I am a bit confused when it says about ‘it needs a callback function and you have to create it’
I have created it
Hint 2, check for the first number that is bigger and return index
On hint 2, a loop would find the biggest number.
FInally, I will return the index with return with 'indexOf(num)
I have researched on Google all of these methods first.

``````function getIndexToIns(arr, num) {
//Sort the array from lowest to biggest with sort(), we can do this by
//asign it to a variable and sort arr
let sortedArr = arr.sort(function(a,b){ //It needs a callback function as mentioned in the hints. It compares each value and sorts it from lowest to biggest

});

return sortedArr.indexOf(num);
}

getIndexToIns([40, 60], 50)
``````
``````
function getIndexToIns(arr, num) {
return num;
}

getIndexToIns([40, 60], 50);

``````

User Agent is: `Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0`.

Challenge: Where do I Belong

I think you need to step out of peaking to the hints and try to visualize the solution first. Just imagine bunch of blocks with numbers right in front of you on the table and one block also with a number outside of the bunch. How would you actually solve it with your hands without taking code into consideration?

1 Like

1 Like

I would put that block with a number outside of the bunch inside with the rest of blocks
Is that a reasonably solution?

but how would you know what’s the correct position? what steps would you take?

The Steps I would take are:

1. Sort the blocks in ascending order, from smallest to largest
2. Include the block at which index should take place
3. Print the index of the block that has been placed inside with the other blocks.
Does this makes sense for you?
I really want to solve this challenge with a correct sol

This is my solution after careful consideration of all the conditions:

``````function getIndexToIns(arr, num) {
//Sort the array from lowest to biggest with sort(), we can do this by
//asign it to a variable and sort arr
let sortedArr = arr.sort(function(a,b){ //It needs a callback function as mentioned in the hints. It compares each value and sorts it from lowest to biggest
//Check the first number that is bigger
if(a > b) {
return indexOf(num) //And return the index
} else {  //Deal with numbers that dont have an index, return the number
return num
}
});
return sortedArr.indexOf(num);
}

getIndexToIns([40, 60], 50);

``````

Feedback welcome, thanks for making me think further and achieve my own sol!

ok, now let’s see how the sort method work…
in freecodecamp we have this challenge that explain the sort method: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/sort-an-array-alphabetically-using-the-sort-method

note though that you actually don’t need to sort the array to solve this challenge but if that’s the way you want to go you need to use `sort` correctly.

1 Like

I get it, we only need to check the index of num, and that can be done with only an if statement.
I will check now this link to see how sort works:)

not really, as you need the index of when the array is sorted. once you manage to make sort work, you can try this other way, for experience

but consider this, the index of the inserted element in the sorted array, isn’t also how many numbers smaller than it are there?

Please explain what is -1 : 1 doing here? in the sort() link challeng
Is my reasoning correct in the comments?
Thank you!

``````function alphabeticalOrder(arr) {
// Only change code below this line
return arr.sort(function (a,b){
//Specify how to return the array items
return a === b ? 0 : a < b ? -1 : 1 //-1 means to sort from last element to first element to sort in ascending order

})

// Only change code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);

``````

you can check the value returned from the function by using `console.log()` around your function call

and -1,1,0 are used by the sort function to decide how to order the elements

1 Like

I dont understand what is -1 , 1 and 0 ?
so that may be why i cannot specify the sort callback function with the return statement.

they are just numbers, but they are used by the sort method how to sort two elements:

JavaScript’s default sorting method is by string Unicode point value, which may return unexpected results. Therefore, it is encouraged to provide a callback function to specify how to sort the array items. When such a callback function, normally called `compareFunction` , is supplied, the array elements are sorted according to the return value of the `compareFunction` : If `compareFunction(a,b)` returns a value less than 0 for two elements `a` and `b` , then `a` will come before `b` . If `compareFunction(a,b)` returns a value greater than 0 for two elements `a` and `b` , then `b` will come before `a` . If `compareFunction(a,b)` returns a value equal to 0 for two elements `a` and `b` , then `a` and `b` will remain unchanged.

You see, the problem with peaking that you’re locking yourself to that solution and all further considerations are based around it. People are notoriously bad in thinking outside the box and peaking is putting yourself in the box. Wouldn’t it be easier just to count all the blocks with numbers that are less than the number on the block outside the bunch without sorting in the first place?