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

举例: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. EasyUI合并行

    扩展一下 EasyUI   下面湿扩展EasyUI 合并行的方法 $.extend($.fn.datagrid.methods, { autoMergeCells: function (jq, fie ...

  2. java环境变量及Eclipse自动编译问题

    环境变量,是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在的完整路 ...

  3. vue指令与组件

    参考http://blog.csdn.net/lioldamon/article/details/74058222?utm_source=itdadao&utm_medium=referral ...

  4. loadrunner配置多台负载机设置

    面对并发量比较大的性能需求,用单台机子进行加压由于本身硬件资源.网络资源等的限制已经不能满足该性能测试条件,这个时候就需要在场景中添加多台负载机来联机做性能测试.添加多台负载机的设置非常简单下面做一个 ...

  5. TOSCA自动化测试工具--怎么写自动化用例

    1.查看一下要测试的对象属性 2.

  6. react-native 解决Could not find method android() for arguments问题

    运行命令行:react-native run-android 报错 Error:(23, 0) Could not find method android() for arguments [****] ...

  7. typeof instanceof 和 obj.constructor

     1.typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: 'number','boolean','string','function'(函数),'object'(NU ...

  8. ls命令输出的文件颜色

    ls的输出颜色不止3种,有以下几种,白色:表示普通文件蓝色:表示目录绿色:表示可执行文件红色:表示压缩文件浅蓝色:链接文件红色闪烁:表示链接的文件有问题 黄色:表示设备文件 灰色:表示其他文件 这是l ...

  9. SQL学习笔记之B+树的几点总结

    本文主要以列表形式将B+树的特点以及注意点等列出来,主要参考<算法导论>.维基百科.各大博客的内容,结合自己的理解写的,如内容有不当之处,请各位雅正. 0x00 前言 B树是为磁盘或其他直 ...

  10. PHP秒杀系统全方位设计分析(一)

    秒杀系统特点人多商品少时间短流量高外挂机器[黄牛和非黄牛] 技术分析瞬间高并发的处理能力多层次的分布式处理能力人机交互与对抗[12306验证码图片] 技术选型分析Linux+Nginx+PHP+Mys ...