0
0

More than 1 year has passed since last update.

【JavaScript関数ドリル】中級編のunion関数の実装のアウトプット

Last updated at Posted at 2021-12-09

課題内容

方針

  • 仮引数で受け取った複数のarrayをループ
  • さらにarrayの中身をループ
  • 新しいarrayに要素をひとつずつpush(同じ要素が含まれていない場合のみ) includesメソッドで判定していく
function union (...arrays){
  let result = [];
  for (let i = 0 ; i < arrays.length ; i ++){
    let currentArray = arrays[i]
    for (let j = 0 ; j < currentArray.length ; j ++){
      // console.log(currentArray)
      if(!result.includes(currentArray[j])){
        result.push(currentArray[j]);
      }
    }
  }
  return result;
}
console.log(union([2], [1,2,3]));
// => [2,1 ,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