I have been scratching my head to find out why if the second not being placed via slice into the first element which is an array.
Is there an issue with
arr[row].splice(col+1,0,num);
?
i used col + 1 since from my understanding, the element to be added would be before that element. col on it’s own would lead to num being placed before col which means the order from smallest to largest won’t be implemented
Your code so far
function getIndexToIns(arr, num) {
arr.sort(function (a, b) { return a - b });
for (var row = 0; row < arr.length; row++) {
for (var col = 0; col < arr[row].length; col++) {
if (num > arr[row][col]) {
arr[row].splice(col + 1, 0, num);
}
}
}
return arr;
}
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/68.0.3440.106 Safari/537.36.
Okay so splice is used here when the if condition is met.
I.E. when inputted num is bigger than an element in the array, splice is used to place num after that element. That way, we end up with an array where num is placed in the correct index such that the array lists elements from smallest to largest