获取数字索引(如果已将其插入到排序数组中) [英] Get index of number if it was inserted into a sorted array

查看:72
本文介绍了获取数字索引(如果已将其插入到排序数组中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< pre class = snippet-code-js lang-js prettyprint-override> function getIndexToIns(arr,num){
arr.sort(function(a,b){
返回a-b;
});

for(var i = 0; i< arr.length; i ++){//遍历数组
if(arr [i]> = num){//数组值大于num
返回i; //返回大于值
的num的索引pos}
else if(arr [i] === undefined){//如果找不到
arr.push(num); //推送至数组
return arr.indexOf(num); //返回新数字的索引pos<-在这种情况下应返回3
}
}
}

console.log(getIndexToIns([2, 5,10],15)); //应该返回3


此函数的任务是对数组进行排序,然后返回 arg2 的索引值(如果在数组中)。


示例: getIndexToIns([10,20 ,30、40、50],35)应该返回 3


我所拥有的麻烦的是,如果在数组中找不到 arg2 ,则将其推入数组并返回其索引值。我似乎无法使其正常工作。

解决方案

另一种方法:

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

当然有几种方法可以执行此操作,但是代码中的修复方法如下:



工作示例

  function getIndexToIns(arr,num){
arr.sort(function(a,b){
return ab;
});
for(var i = 0; i< arr.length; i ++){//循环遍历数组
if(arr [i]> = num){//如果数组值大于num
return i; //返回大于值
的num的索引pos}
if(i === arr.length-1){//如果未找到
arr.push(num); //推送至数组
return arr.indexOf(num); //返回新数字的索引pos<-在这种情况下应返回3
}
}
}

在您的代码中检查了是否(arr [i] === undefined)检查是否在数组的末尾,如果是,那意味着您还没有找到数字,然后可以将其推入并获取索引。


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.

解决方案

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:

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

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.

这篇关于获取数字索引(如果已将其插入到排序数组中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆