Basic Algorithm Scripting - Where do I Belong

Tell us what’s happening:
Describe your issue in detail here.
Hello, I am doing the where do I belong challenge and here is my code. However it says this is incorrect, I changed the .push() method to .concat() and it worked an my question is why doesn’t the code run correctly with the .push() method. I thought it was used to add an element to the end of an array and once that’s done shouldn’t the .sort() be able to sort the array according the the function return value??

Your code so far

function getIndexToIns(arr, num) {
  return arr.push(num).sort((a,b)=> a-b).indexOf(num);
}

getIndexToIns([40, 60], 50);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.5 Safari/605.1.15

Challenge: Basic Algorithm Scripting - Where do I Belong

Link to the challenge:

push method returns new length of the array. concat method returns new array.

return arr.push(num).sort((a,b)=> a-b).indexOf(num);
// will becomes
return 3.sort((a,b)=> a-b).indexOf(num); // error because 3 is not array type.

when you like to chain prototype method, better to check what would be return from previous…

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