Drop it
来源:互联网 发布:手机淘宝 查看评价 编辑:程序博客网 时间:2024/06/05 07:01
题目
让我们来丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止。
第二个参数,func,是一个函数。用来测试数组的第一个元素,如果返回fasle,就从数组中抛出该元素(注意:此时数组已被改变),继续测试数组的第一个元素,如果返回fasle,继续抛出,直到返回true。
最后返回数组的剩余部分,如果没有剩余,就返回一个空数组
要求
drop([1, 2, 3, 4], function(n) {return n >= 3;}) 应该返回 [3, 4]。drop([0, 1, 0, 1], function(n) {return n === 1;}) 应该返回 [1, 0, 1]。drop([1, 2, 3], function(n) {return n > 0;}) 应该返回 [1, 2, 3]。drop([1, 2, 3, 4], function(n) {return n > 5;}) 应该返回 []。drop([1, 2, 3, 7, 4], function(n) {return n > 3;}) 应该返回 [7, 4]。drop([1, 2, 3, 9, 2], function(n) {return n > 2;}) 应该返回 [3, 9, 2]
代码
function drop(arr, func) { // Drop them elements. var newArr = arr.slice(0,arr.length); for(var i = 0; i < arr.length; i++) { if(func(arr[i])) { return newArr; } else { newArr.shift(); } } return [];}drop([1, 2, 3], function(n) {return n < 3; });
阅读全文
0 0
- Drop it
- Drop it
- JS:Drop It!抛弃数组
- drop
- Lesson Thirty-Nine Don't drop it. 别掉了.
- Lesson 39 Don't drop it! 别摔了!
- Cannot drop database "databasename" because it is currently in use.
- PostgreSQL中 drop table指令出现ERROR: cannot drop table userinfo because other objects depend on it
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Error 3724:Cannot drop the table 'object_name' because it is being used for replication.
- Cannot drop the database 'databasename' because it is currently in use
- Cannot drop the database ‘XXX’ because it is being used for replication.
- Lesson 39 Don't drop it! 别摔了…
- Cannot drop the database [NAME] because it is currently in use
- Could not drop object 'student' because it is referenced by a FOREIGN KEY constraint
- Cannot drop the database XXX because it is being used for replication
- 删除数据库时提示数据库正在被使用,无法删除(Cannot drop database databasename because it is currently in use)。
- 053-22 How can you reverse the effects of an ALTER DISKGROUP … DROP DISK command if it has already
- 使用JQuery修改Table特定行的相关属性
- JDBC
- HDU-4027 Can you answer these queries? (线段树 区间修改 区间查询)(The 36th ACM/ICPC Asia Regional Shanghai)
- bzoj4034: [HAOI2015]树上操作
- pytorch model 2 coreml
- Drop it
- 麻麻~我会用小黑鸟自动生成SQL啦 mybatis-generator的使用
- WIN5语音ic注意事项
- jvm调优2
- 菜鸟编程 关于函数的一点总结
- freemarker加载模板文件的
- python: 多属性排序
- HDOJ 1024 DP
- 按之字形顺序打印二叉树