Javascript Insert into array and return index position

function getIndexToIns(arr, num) {
  arr.sort(function(a,b) {
    return a-b;
  });
  for (var i=0;i<arr.length;i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
        return i; // return index pos of num bigger than value
    }
    else if (arr[i] === undefined) { // if not found 
        arr.push(num); // push to array 
        return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}

getIndexToIns([2, 5, 10], 15);

// getIndexToIns ([2, 5, 10], 15) should return 3.

The mission is to sort the array and return the index value (arg2) if it was in the array. EX: getIndexToIns ([10, 20, 30, 40, 50], 35) should return 3.

I am having problems if (arg2) is not found in the array to insert it into it ... and return the index value. I can't seem to get it to work. Any ideas?

+4
source share
6 answers

Another way to do this:

function getIndex(arr, num) {
  return arr.concat(num).sort(function(a, b) {
    return a - b;
  }).indexOf(num);
}

Of course, there are several ways to do this, but the fix in your code is below:

Working example

function getIndexToIns(arr, num) {
  arr.sort(function(a,b) {
    return a-b;
  });
  for (var i=0;i<arr.length;i++) { // cycles through the array
    if (arr[i] >= num) { // if array value is bigger than num 
        return i; // return index pos of num bigger than value
    }
    if (i === arr.length - 1) { // if not found 
        arr.push(num); // push to array 
        return arr.indexOf(num); // return index pos of new num <-- should return 3 in this case
    }
  }
}

, , if (arr[i] === undefined), , , , , , , .

+1

.push .indexOf ?

function arrSort(a, b) {
    return a - b;
}

function getIndexToIns(arr, num) {
    // you sort the array
    arr.sort(arrSort);

    // if it doesn't contain the num
    if(arr.indexOf(num) == -1) {
        // add the num to the array
        arr.push(num);

        // sort the array again
        arr.sort(arrSort);

        // return the index of the num
        return arr.indexOf(num);
    }

    // if the num is in the array, return its position
    return arr.indexOf(num);
}
+1

num , map sort.

function getIndexToIns(arr, num) {
  arr.push(num);
  arr.map(function(a,b) {
    a-b;
     arr.indexOf(num);
  });
  console.log(arr+'\n');
  console.log(num +' is at '+arr.indexOf(num)+'\n');
}

getIndexToIns([2, 5, 10], 15);
Hide result
+1

, , , splice.

function getIndexToIns(arr, num) {
  var index = (function search(from, to) {
    if(from == to) return to;
    var m = Math.floor((from+to)/2);
    if(arr[m] > num) return search(from, m);
    if(arr[m] < num) return search(m+1, to);
    return m;
  })(0, arr.length);
  arr.splice(index, 0, num);
  return index;
}

, , :

function getIndexToIns(arr, num) {
  for(var i=arr.length; i>0 && arr[i-1]>num; --i) arr[i] = arr[i-1];
  arr[i] = num;
  return i;
}
+1
function getIndexToIns(arr, num) {
    function compare(a,b){
        return  a-b;
    }
    arr.push(num);
    arr.sort(compare);
    console.log(num);
    num = arr.indexOf(num);
    return num;
}

getIndexToIns ([40, 60], 50);

0

I used for with nested two if conditions counting the indices in the variables j and k .. p>

function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  var j = 0;
  var k = 0;
  
  arr.sort(function(a, b){return a - b;});
  for (var i= 0; i < arr.length; i++){
    if ( arr[i] < num ) {
      k++;      
      if (arr[i] > num) {
        j++;
      }
    }
  }
  return j+k;
}
console.log(getIndexToIns([3, 10, 5], 3));
console.log(getIndexToIns([10, 20, 30, 40, 50], 35));
console.log(getIndexToIns([10, 20, 30, 40, 50], 30));
console.log(getIndexToIns([40, 60], 50));
console.log(getIndexToIns([5, 3, 20, 3], 5));
console.log(getIndexToIns([2, 20, 10], 19));
console.log(getIndexToIns([2, 5, 10], 15));
Run codeHide result
0
source

All Articles