Reputation: 99
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
}
}
}
console.log(getIndexToIns([2, 5, 10], 15)); // Should return 3
The mission of this is to sort an array, and return the index value of arg2
if it were in the array.
Example: getIndexToIns([10, 20, 30, 40, 50], 35)
should return 3
.
What I’m having trouble with, is if arg2
is not found in the array, to push it into it and return its index value. I can’t seem to make it work.
Upvotes: 7
Views: 2668
Reputation: 1537
I used for with nested two if conditions witch counting indexes in variables j and k.
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));
Upvotes: 0
Reputation: 44010
You could just push num
into the array then sort it out with map
or 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);
Upvotes: 1
Reputation: 586
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);
Upvotes: 0
Reputation: 288510
Since your arrays seem already sorted, you should just use a dichotomic search to find the index, and then insert it with 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;
}
Or, since it will be linear anyways, loop backwards manually:
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;
}
Upvotes: 1
Reputation: 576
Why don't you use the .push
and .indexOf
methods on the array?
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);
}
Upvotes: 1
Reputation: 13896
Another way to do it:
function getIndex(arr, num) {
return arr.concat(num).sort(function(a, b) {
return a - b;
}).indexOf(num);
}
Sure there a few ways to do this but the fix in your code is below:
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
}
}
}
in your code you checked to see if (arr[i] === undefined)
that will never happen, so instead check to see if you are at the end of the array, and if so, that means you haven't found your number, and then you can push it and get the index.
Upvotes: 2