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);
1
  • 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. Commented Jan 2, 2020 at 4:51

7 Answers 7

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Checking for -1 from findIndex() and returning the array length might be a nice touch.
@MarkMeyer nice idea, I've added that in
@RushikeshGanesh If the answer resolved your issue, please mark it as accepted by clicking on the grey checkmark on the left
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);

Comments

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;
}

Comments

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>

Comments

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)
)

Comments

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));

Comments

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)
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.