freeCodeCamp/curriculum/challenges/japanese/02-javascript-algorithms-an.../basic-data-structures/copy-an-array-with-the-spre...

3.1 KiB

id title challengeType forumTopicId dashedName
587d7b7b367417b2b2512b13 スプレッド演算子による配列のコピー 1 301157 copy-an-array-with-the-spread-operator

--description--

slice() を使用すると、コピーする配列の要素を選択できますが、他にもいくつかの便利な機能があり、ES6の新しいスプレッド演算子を使用すれば、簡単で非常に読みやすい構文で、配列のすべての要素を順番通りにコピーすることができます。 スプレッドの構文は「...」のように単純なものです。

実際には、スプレッド演算子を使用して、以下のように配列をコピーすることができます。

let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];

thatArray[true, true, undefined, false, null] になります。 thisArray は変更されず、thatArray には thisArray と同じ要素が含まれます。

--instructions--

関数 copyMachine を定義しました。この関数は引数として arr (配列) と num (数値) を受け取ります。 この関数では、arrnum 個のコピーで構成される新しい配列を返す必要があります。 必要なものはほぼ揃っていますが、まだうまく機能しません。 スプレッド構文を使用して、正しく機能するように関数を変更してください (ヒント: すでに説明した別のメソッドがここで役に立つかもしれません)。

--hints--

copyMachine([true, false, true], 2)[[true, false, true], [true, false, true]] を返す必要があります。

assert.deepEqual(copyMachine([true, false, true], 2), [
  [true, false, true],
  [true, false, true]
]);

copyMachine([1, 2, 3], 5)[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] を返す必要があります。

assert.deepEqual(copyMachine([1, 2, 3], 5), [
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3],
  [1, 2, 3]
]);

copyMachine([true, true, null], 1)[[true, true, null]] を返す必要があります。

assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);

copyMachine(["it works"], 3)[["it works"], ["it works"], ["it works"]] を返す必要があります。

assert.deepEqual(copyMachine(['it works'], 3), [
  ['it works'],
  ['it works'],
  ['it works']
]);

copyMachine 関数では、配列 arr を付けた spread operator を使用する必要があります。

assert(code.match(/\.\.\.arr/));

--seed--

--seed-contents--

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // Only change code below this line

    // Only change code above this line
    num--;
  }
  return newArr;
}

console.log(copyMachine([true, false, true], 2));

--solutions--

function copyMachine(arr,num){
    let newArr=[];
    while(num >=1){
    newArr.push([...arr]);
    num--;
    }
    return newArr;
}
console.log(copyMachine([true, false, true], 2));