1

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

this is my code

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr = arr.sort();
  num = Math.floor(num);
  for(let i=0;i<arr.length;i++){
  }
  return num;
}

getIndexToIns([690, 60], 59);
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Rushikesh Ganesh
  • 290
  • 2
  • 16
  • What have you tried? What if the number is less than all in the array? Simplest IMO would be to `if (arr[i] > num) return i - 1;` in your loop, which returns -1 if `num` is less than all the entries. – Drew Reese Jan 02 '20 at 04:51

7 Answers7

6

You can use .findIndex() to find the first element in your sorted array which is larger than your passed num element:

const getIndexToIns = (arr, num) => {
  const res = arr.sort((a, b) => a-b).findIndex(n => n > num);
  return res === -1 ? arr.length : res;
}

console.log(getIndexToIns([1,2,3,4], 1.5));
console.log(getIndexToIns([20,3,5], 19));
console.log(getIndexToIns([20,3,5], 100)); // 3 (array length)
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • Checking for `-1` from `findIndex()` and returning the array length might be a nice touch. – Mark Jan 02 '20 at 04:59
  • 1
    @MarkMeyer nice idea, I've added that in – Nick Parsons Jan 02 '20 at 05:02
  • 1
    @RushikeshGanesh If the answer resolved your issue, please [mark it as accepted](https://meta.stackexchange.com/a/5235/289255) by clicking on the grey checkmark on the left – adiga Jan 02 '20 at 05:50
1

Here the solution

function getIndexToIns(arr, num) {
  var counter = 0;
  // Find my place in this sorted array.
  arr = arr.sort();
  num = Math.floor(num);

  for (let i = 0; i < arr.length; i++) {
    if (num > arr[i]) {
      counter++
    } else {
      break;
    }
  }
  return counter;
}

var count = getIndexToIns([690, 60, 55, 2], 59);
console.log(count);
adiga
  • 34,372
  • 9
  • 61
  • 83
saifur
  • 637
  • 4
  • 17
0

This is like one small part of insertion sort (which you can read about here). What you are trying to do is to just compare the num with each element of sorted array arr, until you find a number which is greater than num.

So it'd look something like this:

for(let i=0;i<arr.length;i++){
    if(num <= arr[i])
        return i;
}
Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
0

here is a code you may try

function getIndexToIns(a, b) {
  let ind = 0;
  for (var i = 0; i < a.length; i++) {
    if (b < a[i]) {
      ind++;
    }
  }
  return ind
}

console.log(getIndexToIns([20, 3, 5], 19));
adiga
  • 34,372
  • 9
  • 61
  • 83
Alex
  • 818
  • 1
  • 8
  • 17
0

function getIndexToIns(arr, num) {
    var return_val;
  // Find my place in this sorted array.
  arr = arr.sort();
  num = Math.floor(num);
  for(let i=0;i<arr.length;i++){
    if(arr[i]>num){
        return_val = arr[i-1];
    }
  }
 return return_val;
}

var return_value = getIndexToIns([690, 60], 59);
console.log(return_value);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
0

Because arr.sort() sorting does not sort the array accurately, check this: Array.sort() doesn't sort numbers correctly

This code is working fine

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.sort(function(a, b) { return a - b; });
  num = Math.floor(num);
  //console.log(arr)
  var found = arr.findIndex(function(e) {
    return e > num;
  });
  return found;
}

console.log(
  getIndexToIns([1, 2, 3, 4], 1.5),
  getIndexToIns([20, 3, 5], 19),
  getIndexToIns([20, 3, 5], 19)
)
adiga
  • 34,372
  • 9
  • 61
  • 83
Mohamad A Sallal
  • 614
  • 6
  • 12
0

Try this

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var index = 0
  arr.sort((a, b) => a - b)
  for (let i = 0; i < arr.length; i++) {
    if (num > arr[i]) {

    } else {
      index = i
      break
    }
  }
  return index;
}

console.log(
  getIndexToIns([1, 2, 3, 4], 1.5)
)
adiga
  • 34,372
  • 9
  • 61
  • 83
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44