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

4.1 KiB

id title challengeType videoUrl localeTitle
587d7b7b367417b2b2512b13 Copy an Array with the Spread Operator 1 نسخ صفيف مع المشغل انتشار

Description

بينما تسمح لنا slice() بأن نكون انتقائيين حول عناصر المصفوفة المراد نسخها ، من بين العديد من المهام المفيدة الأخرى ، يتيح لنا مشغل التوزيع الجديد لـ ES6 نسخ جميع عناصر الصفيف بسهولة ، بالترتيب ، مع بناء بسيط وقابل للقراءة للغاية. تبدو صيغة الانتشار بهذا الشكل: ... الناحية العملية ، يمكننا استخدام عامل الانتشار لنسخ مصفوفة مثل:
السماح لهذاالصورة = [true، true، undefined، false، null]؛
السماح أن AArray = [... thisArray]؛
// thatArray يساوي [true ، true ، غير محدد ، false ، فارغ]
/ / هذا لا يزال يظل بدون تغيير ، وهو مطابق لذلك

Instructions

قمنا بتعريف وظيفة ، copyMachine والتي تأخذ arr (صفيف) و num (a number) كوسيطة. من المفترض أن تقوم الدالة بإرجاع صفيف جديد يتكون من نسخ num من arr . لقد قمنا بمعظم العمل لك ، لكنه لا يعمل بشكل صحيح بعد. قم بتعديل الوظيفة باستخدام صيغة الانتشار بحيث تعمل بشكل صحيح (تلميح: قد تكون طريقة أخرى قمنا بتغطيتها بالفعل مفيدة هنا!).

Tests

tests:
  - text: '<code>copyMachine([true, false, true], 2)</code> <code>[[true, false, true], [true, false, true]]</code>'
    testString: 'assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]], "<code>copyMachine([true, false, true], 2)</code> should return <code>[[true, false, true], [true, false, true]]</code>");'
  - text: '<code>copyMachine([1, 2, 3], 5)</code> <code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>'
    testString: 'assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], "<code>copyMachine([1, 2, 3], 5)</code> should return <code>[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]</code>");'
  - text: '<code>copyMachine([true, true, null], 1)</code> يجب أن يعيد <code>[[true, true, null]]</code>'
    testString: 'assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]], "<code>copyMachine([true, true, null], 1)</code> should return <code>[[true, true, null]]</code>");'
  - text: '<code>copyMachine([&quot;it works&quot;], 3)</code> <code>[[&quot;it works&quot;], [&quot;it works&quot;], [&quot;it works&quot;]]</code>'
    testString: 'assert.deepEqual(copyMachine(["it works"], 3), [["it works"], ["it works"], ["it works"]], "<code>copyMachine(["it works"], 3)</code> should return <code>[["it works"], ["it works"], ["it works"]]</code>");'
  - text: و <code>copyMachine</code> وظيفة يجب الاستفادة من <code>spread operator</code> مع مجموعة <code>arr</code>
    testString: 'assert.notStrictEqual(copyMachine.toString().indexOf(".concat(_toConsumableArray(arr))"), -1, "The <code>copyMachine</code> function should utilize the <code>spread operator</code> with array <code>arr</code>");'

Challenge Seed

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

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

// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));

Solution

// solution required