1
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関数ドリル】Lodash関数の実装【勉強用】_.last関数

Last updated at Posted at 2022-02-23

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

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

配列の末尾を返す
##last関数の課題内容
https://youtu.be/bPJjIawWFBI

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

  • array.length-1を変数に格納するだけでよさそう
  • 関数ドリルの取り組む順番を間違えている気がする

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

  • そのままarray[array.length -1]を返せば1行で済んだ
  • 今後も元の配列を破壊していないか確認することにする

##last関数の実装コード

const last = (array) => {
    const lastNum = array.length - 1;
    return array[lastNum];
};

const nums = [1, 2, 3, 4, 5]
console.log(last(nums));
// => 5

console.log(nums);
// => [1, 2, 3, 4, 5]

##last関数の解答コード

function last(array) {
  return array[array.length - 1];
}

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

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
1
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?