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.
// getIndexToIns([2,5,10],15)应该返回3。
The mission of this is to sort an array, and return the index value of (arg2) if it were in the array. EX: getIndexToIns([10, 20, 30, 40, 50], 35) should return 3.
这样做的任务是对数组进行排序,并返回(arg2)的索引值(如果它在数组中)。 EX:getIndexToIns([10,20,30,40,50],35)应该返回3。
What I'm having trouble with, is if the (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. Any ideas?
我遇到的问题是,如果在数组中找不到(arg2),将其推入它...并返回其索引值。我似乎无法使其发挥作用。有任何想法吗?
6 个解决方案
#1
1
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.
在你的代码中你检查是否(arr [i] === undefined)永远不会发生,所以请检查你是否在数组的末尾,如果是,那就意味着你没有找到你的数字,然后你可以推它并获得索引。
#2
1
Why don't you use the .push
and .indexOf
methods on the array?
为什么不在阵列上使用.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);
}
#3
1
Since your arrays seem already sorted, you should just use a dichotomic search to find the index, and then insert it with splice
.
由于您的数组似乎已经排序,您应该使用二分搜索来查找索引,然后使用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;
}
#4
1
You could just push num
into the array then sort it out with map
or sort
.
您可以将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);
#5
0
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);
getIndexToIns([40,60],50);
#6
0
I used for with nested two if conditions witch counting indexes in variables j and k.
如果条件计算变量j和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));
#1
1
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.
在你的代码中你检查是否(arr [i] === undefined)永远不会发生,所以请检查你是否在数组的末尾,如果是,那就意味着你没有找到你的数字,然后你可以推它并获得索引。
#2
1
Why don't you use the .push
and .indexOf
methods on the array?
为什么不在阵列上使用.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);
}
#3
1
Since your arrays seem already sorted, you should just use a dichotomic search to find the index, and then insert it with splice
.
由于您的数组似乎已经排序,您应该使用二分搜索来查找索引,然后使用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;
}
#4
1
You could just push num
into the array then sort it out with map
or sort
.
您可以将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);
#5
0
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);
getIndexToIns([40,60],50);
#6
0
I used for with nested two if conditions witch counting indexes in variables j and k.
如果条件计算变量j和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));