当前位置: 首页 > news >正文

0052【Edabit ★☆☆☆☆☆】Learn Lodash: _.drop, Drop the First Elements of an Array

0052【Edabit ★☆☆☆☆☆】Learn Lodash: _.drop, Drop the First Elements of an Array

arrays

Instructions

According to the lodash documentation, _.drop creates a slice of an array with n elements dropped from the beginning.

Your challenge is to write your own version using vanilla JavaScript.

Examples
drop([1, 2, 3], 1) // [2, 3]
drop([1, 2, 3], 2) // [3]
drop([1, 2, 3], 5) // []
drop([1, 2, 3], 0) // [1, 2, 3]
Notes
  • Do not attempt to import lodash; you are simply writing your own version.
Solutions
function drop(arr, val = 1) {while(val-->0){arr.shift();}return arr;
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertSimilar(drop([1, 2, 3], 2), [3])
Test.assertSimilar(drop([1, 2, 3], 5), [])
Test.assertSimilar(drop([1, 2, 3], 0), [1, 2, 3])
Test.assertSimilar(drop(["banana", "orange", "watermelon", "mango"], 2), ["watermelon", "mango"])
Test.assertSimilar(drop([], 2), [])
http://www.lryc.cn/news/210604.html

相关文章:

  • 从无人机到实景三维海洋系统
  • 第二章前端开发ES6基础
  • 使用Python将PDF转为图片
  • JAVA 同城服务货运搬家小程序系统开发时应注意哪些?
  • uniapp开发小程序 小米手机真机bottom:0无效 底部间隙 设备安全区域处理办法
  • 四十、【进阶】索引失效情况2
  • 敏感词过滤--golang
  • NVIDIA cuda安装时全部失败
  • APScheduler-调度器AsyncIOScheduler
  • 二十三种设计模式全面解析-原型模式(Prototype Pattern)详解:创造对象的奇妙之道
  • el-input 给icon图标绑定点击事件
  • 计算机网络——物理层
  • 常用排序算法
  • MGRE环境下的OSPF
  • 【计算机毕设】微信小程序案例-学生签到系统
  • 【数据分析】上市公司半年报数据分析
  • 【C++初阶】类和对象——操作符重载const成员函数取地址重载日期类的实现
  • JAVA中的垃圾回收器(2)
  • mac 安装homebrew ,golang
  • 李沐——论文阅读——VIT(VIsionTransformer)
  • uniapp表单验证
  • Crawler4j实例爬取爱奇艺热播剧案例
  • uniapp项目APP端安卓ios权限检测教程
  • java多进程间(父进程与子进程)通信
  • 【从0到1设计一个网关】整合Nacos-服务注册与服务订阅的实现
  • 【uniapp】短信验证码输入框
  • 负载均衡的综合部署练习(hproxy+keepalived和lvs-DR+keepalived+nginx+Tomcat)
  • 设计模式——策略模式(Strategy Pattern)+ Spring相关源码
  • ORB-SLAM3算法2之开源数据集运行ORB-SLAM3生成轨迹并用evo工具评估轨迹
  • Qt 序列化函数和反序列化函数