FreeCodeCamp学习--Where do I belong
来源:互联网 发布:ios视频制作软件 编辑:程序博客网 时间:2024/05/16 08:20
(注意:sort()默认的是以按照转换为的字符串的诸个字符的Unicode位点进行排序。)
我身在何处?
先给数组排序,然后找到指定的值在数组的位置,最后返回位置对应的索引。
举例: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
。
function where(arr, num) { // Find my place in this sorted array. var arr1=[]; arr1=arguments[0]; arr1.sort(function(a,b){ return a-b; }); if(num>arr1[arr1.length-1]) return arr1.length; for(var i=0;i<arr1.length;i++) { if(arr1[i]>=num){ return i; } }}where([5, 3, 20, 3], 5);
0 0
- FreeCodeCamp学习--Where do I belong
- where do i belong
- Where do I belong
- Bonfire: Where do I belong
- Where do I belong 重点:arr.sort(compareFunction)
- Where I belong
- FCC--Where do I belong(排序并寻找数字的下标)
- where do I turn?
- A. Where do I Turn?
- Where Do I LOOK Now?
- CodeForces 227A Where do I Turn?
- I would like to learn PFC. Where Do I start?
- I want to learn Android Development, where do I start?
- Where do I find more information about XML?
- codeforces #140 dv2 A where do I turn
- Codeforces 227A Where do I Turn?(代数)
- Codeforces Round #140 (Div. 2) A. Where do I Turn?
- Somewhere I Belong--LINKIN PARK
- python 编码 乱码问题
- memset赋值0和-1,还能赋其他值吗?
- 二叉树基础题(三)
- 树莓派+RC522模块
- 4月8日,DemogetProperties,每日20行。
- FreeCodeCamp学习--Where do I belong
- 博主github项目分享
- 震动传感器设计
- 服务发现
- The Galactic Olympics ///dp
- Centos7修改ssh端口
- visual studio 2010 + sdl2 + glm + glew安装
- 网易2017春招笔试 分饼干
- jQuery之简单的表单验证