Return the remaining elements of an array after chopping off
n elements from the head.
The head means the beginning of the array, or the zeroth index.
Review: splice();
Solution 1: 使用splice()
Thinking process: (concept is way more important than fact!!)
1. splice(index(position),howmany)的用法就是At index position , remove howmany items.
Code:
function slasher(arr,howmany){
arr.splice(0,howmany); // position index = 0 , remove howmany = howmany;
return arr;
}
slasher([1, 2, 3], 2);
slasher([1, 2, 3], 2) should return [3].slasher([1, 2, 3], 0) should return [1, 2, 3].slasher([1, 2, 3], 9) should return [].slasher(["burgers", "fries", "shake"], 1) should return["fries", "shake"].
Solution 2: 使用slice();
Thinking process:
1. slice(a,b)截取從a~b(以前)這段型成新的array
Code:
function(arr, howmany){
return arr.slice(howmany,arr.length); // beginning = howmany end = arr.length
}
沒有留言:
張貼留言