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

举例: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. 一个简单的ssm项目

    准备说明jdk.tomcat.idea.mave配置请看我前两篇,这里说下mysql以及我的mysql图像化工具 数据库 项目概览 项目构建--------搭建一个简单的mave的web项目,构建步骤 ...

  2. 使用LinQ进行增删改查

    数据库访问技术: ADO.net EF框架 LinQ LinQ是一种高集成化的数据库访问技术,他将数据库中的表映射成程序中的类 数据库的表名变成类名 数据库的列名变成字段名/属性名 所有的操作都是通过 ...

  3. TOSCA自动化测试工具--识别元素唯一性的控件

    当Modules模块通过Scan识别出页面元素后,选择需要测试的对象,然后判断对象唯一性

  4. JVM内存结构 JVM的类加载机制

    JVM内存结构: 1.java虚拟机栈:存放的是对象的引用(指针)和局部变量 2.程序计数器:每个线程都有一个程序计数器,跟踪代码运行到哪个位置了 3.堆:对象.数组 4.方法区:字节流(字节码文件) ...

  5. lightGallery 一个视屏不播放 解决方法

    这次使用了lightGallery,感觉还不错.样式比较美观,并且支持响应式. 使用过程中,我遇到了下面的问题: 当我 .picsgallery里面只有一个 .gItem的时候.点击弹出幻灯片,再点击 ...

  6. 20145301实验四 Android开发基础

    20145301<Java程序设计>实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.26 15:30- ...

  7. RocEDU.阅读.写作《乌合之众》(四)

    第三卷 不同群体的分类及特点 第二章 被称为犯罪群体的群体 通常,群体犯罪的动机是暗示,参与人认为自己是在履行责任,和平常的犯罪大不相同.犯罪者服从于别人的怂恿,而这种力量在群体中格外强大,犯罪者受到 ...

  8. Visual Studio 2010生成解决方案时,导致C盘空间越来越小

    为了从根本上解决问题,还是去掉智能跟踪选项吧,方案: VS2010-->工具-->选项-->IntelliTrance-->将“启用IntelliTrace”勾选去掉--> ...

  9. hive 相关异常

    内存不够: set mapreduce.map.memory.mb=4096;set mapreduce.map.java.opts=-Xmx3280m; set mapreduce.reduce.m ...

  10. LeetCode——Find Duplicate Subtrees

    Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...