[feedback on code] Where Do I Belong?

[feedback on code] Where Do I Belong?
0

#1

Just completed this challenge completely on my own without looking for clues online so I’m pretty stoked. Would love to get some feedback on my code as to how I can write it better or more efficiently.

function getIndexToIns(arr, num) {
var sorted = arr.sort(function(a, b){
return a - b;
});
for (var i = 0;i<sorted.length;i++) {
if (num > sorted[i] && num < sorted[i+1])
sorted.splice(i+1, 0, num);
else if (num > sorted[i]) {
sorted.push(num);
} else {
return sorted.indexOf(num);
}
}
}

thanks


#2

It looks like it will work well enough. Reading it is not too bad. You really aren’t going to get too much more efficient. You could of used .map(function(item, index){…}) but honestly it is much slower (about 30%) so I see no problems.