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

Posted at

##fill関数の課題内容
詳細はこちら
   ↓
https://js-drills.com/blog/fill/

##fill関数の取り組む前の状態
最後の

console.log( fill([4, 6, 8, 10], '*', 1, 3) );

をどうすればいいのかわからづ悩んでいた状態

##fill関数に取り組んだ後の状態
難しくはなかったが発想が出てこないです。

##fill関数の実装コード(答えを見る前)
分かりませんでした。

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

function fill(array, value, start = 0, end = array.length) {
  for(let i = 0; i < array.length; i++) {
    if(start <= i && i < end) {
      array[i] = value;
    }
  }

  return array;
}

var array = [1, 2, 3];
console.log( fill(array, 'a') );
// => ['a', 'a', 'a']

console.log( fill(Array(3), 2) );
// => [2, 2, 2]

console.log( fill([4, 6, 8, 10], '*', 1, 3) );
// => [4, '*', '*', 10]
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?