0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascript関数ドリル 初級編drop関数の実相のアウトプット

Last updated at Posted at 2020-09-11

##drop関数の課題内容
   ↓
https://js-drills.com/blog/drop/

##drop関数の取り組む前の状態
functionの中をどうすればいいのかわからないがとりあえずコードを書こうとした

##drop関数に取り組んだ後の状態
size=1のところが理解できないです。

##drop関数の実装コード(答えを見る前)


function drop (array,number){
    const dropArray=[]
for(let number=0;){}
return dropArray
}

// _.drop([1, 2, 3], 0);

// // => [1, 2, 3]

##drop関数の実装コード(答えを見た後)

function drop(array, size = 1) {
  if (size === 0) {
    return [...array];
  }
  if (array.length <= size) {
    return [];
  }

  const droppedArray = [];
  for(let i = size; i < array.length; i++) {
    droppedArray.push( array[i] );
  }

  return droppedArray;
}


const numbers = [1, 2, 3];

console.log( drop(numbers), numbers );
// => [2, 3]

// _.drop([1, 2, 3], 2);
console.log( drop(numbers, 2), numbers );
// // => [3]

// _.drop([1, 2, 3], 5);
console.log( drop(numbers, 5), numbers );
// // => []

// _.drop([1, 2, 3], 0);
console.log( drop(numbers, 0), numbers );
// // => [1, 2, 3]
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?