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 1 year has passed since last update.

【JavaScript関数ドリル】を毎日やる【勉強用】その6 tail関数

Posted at

#【JavaScript関数ドリル】初級編のtail関数の実装のアウトプット

tail関数の挙動
_.tail([1, 2, 3]);
// => [2, 3]

先頭を取り除いたものを新たに出力する
##tail関数の課題内容

##tail関数に取り組む前の状態

  • 元の配列を変更しないよう気をつける

##tail関数に取り組んだ後の状態

  • 数分で作成できた
  • 変数名の付け方もnewばかりでなく、関数名を入れた方がわかりやすいと気づいた
  • もっとサクッとfor文を使えるようになる

##tail関数の実装コード

const tail = (array) => {
    const newArray = array.slice();
    newArray.shift();
    return newArray;
};

console.log(tail([1, 2, 3]));
// =>[2,3]

##tail関数の解答コード

function tail(array) {
  const tailArray = [];
  for(let i = 1; i < array.length; i++) {
    tailArray.push( array[i] );
  }

  return tailArray;
}

console.log( tail([1, 2, 3]) );
// => [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?