要求:
打不死的小强!
返回一个数组被截断n个元素后还剩余的元素,截断从索引0开始。
结果:
-
slasher([1, 2, 3], 2)
应该返回[3]
.
-
slasher([1, 2, 3], 0)
应该返回[1, 2, 3]
.
-
slasher([1, 2, 3], 9)
应该返回[]
.slasher([1, 2, 3], 4)
应该返回[]
.
-
slasher(["burgers", "fries", "shake"], 1)
应该返回["fries", "shake"]
.
-
slasher([1, 2, "chicken", 3, "potatoes", "cheese", 4], 5)
应该返回["cheese", 4]
.
代码:
方法一:使用slice()
//Array.slice() function slasher(arr, howMany) {
// it doesn't always pay to be first
var newarr; if(arr.length>howMany){
newarr=arr.slice(howMany);
return newarr;
}else{
newarr=[];
return newarr;
}
//return arr;
} slasher([1, 2, 3], 2);
方法二:使用splice()
function slasher(arr, howMany) {
// it doesn't always pay to be first
arr.splice(0,howMany); return arr;
}