前端Freecode camp

Slasher Flick -- Freecodecamp

2017-03-24  本文已影响29人  Marks

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.
TEST☟
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([1, 2, 3], 4) should return [].
slasher(["burgers", "fries", "shake"], 1) should return ["fries", "shake"].
slasher([1, 2, "chicken", 3, "potatoes", "cheese", 4], 5) should return ["cheese", 4].

无头绪??

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  
  if(arr.length>howMany){
    return arr.splice(0,howMany);
  }else {
    var a = [];
    return a;
  }
}
slasher([1, 2, 3], 2);```

//方法1:splice()
function slasher(arr, howMany) {
// it doesn't always pay to be first
arr.splice(0,howMany);
return arr;
}
console.log(slasher([1, 2, 3], 2));```

//方法2:slice()
function slasher(arr, howMany) {
  return arr.slice(howMany);
}
slasher([1, 2, 3], 2);```


https://github.com/freeCodeCamp/freeCodeCamp/wiki/Algorithm-Chunky-Monkey
上一篇 下一篇

猜你喜欢

热点阅读