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関数ドリル 初級編lastOf関数の実相のアウトプット

Posted at

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

##lastOf関数の取り組む前の状態
for分をどうすればいいのか悩んだ

##lastOf関数に取り組んだ後の状態
let i = fromIndex; 0 <= i; i--
このように±を逆にできることを知れた。

##lastOf関数の実装コード(答えを見る前)
実行できませんでした
##last関数の実装コード(答えを見た後)


function lastIndexOf(array, value, fromIndex = array.length - 1) {
  for(let i = fromIndex; 0 <= i; i--) {
    if(array[i] === value) {
      return i;
    }
  }

  return -1;
}

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

// Search from the `fromIndex`.
console.log( lastIndexOf([1, 2, 1, 2], 2, 2) );
// => 1
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?