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

举例: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

这是一些对你有帮助的资源:

思路就是先把数值插入数组,然后排序,找出插入的数在新数组的索引。    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。

function where(arr, num) {
arr.push(num); //插入数组
return arr.sort(function(f,s){
return f-s; //排序从小到大
}).indexOf(num);
}

Where do I belong的更多相关文章

  1. 解决在使用client object model的时候报“object does not belong to a list”错误

    在查看别人代码的时候,发现了个有意思的问题,使用client object model将一个文件check in 我使用的是如下语句获取file Microsoft.SharePoint.Client ...

  2. Cannot uninstall 'html5lib'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    如标题,安装Tensorflow-gpu时遇到的完整问题 Cannot uninstall 'html5lib'. It is a distutils installed project and th ...

  3. Cannot uninstall 'pyserial'. It is a distutils installed project and thus we cannot a ccurately determine which files belong to it which would lead to only a partial uninstall. 解决方法

    最近再升级 pyserial模块时,采用 pip install --upgrade pyserial,待模块下载完成准备卸载原版本时 提示:“Cannot uninstall 'pyserial'. ...

  4. python Cannot uninstall 'numpy'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    在Python中移除(升级)numpy的时候出现: Cannot uninstall 'numpy'. It is a distutils installed project and thus we ...

  5. All flavors must now belong to a named flavor dimension

    FAQ: All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/ ...

  6. Error:All flavors must now belong to a named flavor dimension.

    环境 android studio 3.0 错误 Error:All flavors must now belong to a named flavor dimension. 解决 在build.gr ...

  7. Cannot uninstall 'enum34'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    更新tensorflow时遇到报错 Found existing installation: enum34 1.0.4Cannot uninstall 'enum34'. It is a distut ...

  8. Country roads take me home, to the place I belong.

    Country roads take me home, to the place I belong.故乡的路,带我回家吧,回到我期盼已久的归宿.

  9. ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    pip 安装 docker库报错: ERROR: Cannot uninstall 'chardet'. It is a distutils installed project and thus we ...

  10. 安卓 android studio 报错 All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com

    这个问题是Android studio升级到3.0之后,运行的时候会提示gradle要升级到3.5版本才能编译.于是我把我的gradle升级到了 gradle-4.1-milestone-1 版本,是 ...

随机推荐

  1. mysql锁机制之乐观锁(二)

    select for update: 在执行这个 select 查询语句的时候,会将对应的索引访问条目进行上排他锁(X 锁),也就是说这个语句对应的锁就相当于update带来的效果. select * ...

  2. HDU 4585 Shaolin(map应用+二分)

    题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进 ...

  3. Drools 规则引擎环境搭建

    一.关于 drools 规则引擎 前面写过一篇 Drools 规则引擎相关的文章,这篇文章主要记录一下规则引擎的环境搭建和简单示例.不熟悉 drools 的朋友可以看看这篇文章: 自己写个 Drool ...

  4. HDU - 5909 Tree Cutting (树形dp+FWT优化)

    题意:树上每个节点有权值,定义一棵树的权值为所有节点权值异或的值.求一棵树中,连通子树值为[0,m)的个数. 分析: 设\(dp[i][j]\)为根为i,值为j的子树的个数. 则\(dp[i][j\o ...

  5. iOS 学习 RESTful 中 Http 的幂等性

    一. RESTful  RESTful (Representational State Transfer) 是一种常用流行的软件架构,设计风格或协议标准.提供了一组设计风格和约束条件.主要用于客户端和 ...

  6. i.MX 6Q开发环境配置

    #适用于 Ubuntu 14.04 x64   imx6qdl-cubox-i.dtsi   #更新系统 sudo apt-get update sudo apt-get upgrade   #安装基 ...

  7. PreparedStatement和Statement区别详解

    技术原理 该 PreparedStatement接口继承Statement,并与之在两方面有所不同: PreparedStatement 实例包含已编译的 SQL 语句.这就是使语句“准备好”.包含于 ...

  8. AVAudioSession(1):iOS Audio Session 概览

    本文转自:AVAudioSession(1):iOS Audio Session 概览 | www.samirchen.com 本文内容主要来源于 Audio Session Programming ...

  9. union遇上ntext数据类型

    http://www.myhack58.com/Article/html/3/7/2011/31392.htm

  10. 【C#】枚举和字符串以及数字之间的互相转换

    准备条件: ①枚举类型: public enum enumColor { Red = , Yellow, Green, Blue, White, Black } ②以下状态都是理想状态,并未对错误数据 ...