JavaScript基础算法——数组排序并找出元素索引

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/funkstill/article/details/88086705

要求:

先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引。

举例:where([1,2,3,4], 1.5) 应该返回 1。因为1.5插入到数组[1,2,3,4]后变成[1,1.5,2,3,4],而1.5对应的索引值就是1

同理,where([20,3,5], 19) 应该返回 2。因为数组会先排序为 [3,5,20]19插入到数组[3,5,20]后变成[3,5,19,20],而19对应的索引值就是2

样本:

where([10, 20, 30, 40, 50], 35)应该返回 3.

where([10, 20, 30, 40, 50], 30)应该返回 2.

where([40, 60], 50) 应该返回 1.

where([3, 10, 5], 3) 应该返回 0.

where([5, 3, 20, 3], 5) 应该返回 2.

where([2, 20, 10], 19) 应该返回 2.

where([2, 5, 10], 15) 应该返回 3.

解法:

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

where([40, 60], 50);
扫描二维码关注公众号,回复: 5509785 查看本文章

猜你喜欢

转载自blog.csdn.net/funkstill/article/details/88086705