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.
Please help
Your code so far
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);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0.
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?
Sort the blocks in ascending order, from smallest to largest
Include the block at which index should take place
Print the index of the block that has been placed inside with the other blocks.
Does this makes sense for you?
Please provide your insights
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!
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.
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"]);
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?