freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../functional-programming/add-elements-to-the-end-of-...

3.1 KiB

id title challengeType videoUrl localeTitle
587d7da9367417b2b2512b67 Add Elements to the End of an Array Using concat Instead of push 1 إضافة عناصر إلى نهاية صفيف باستخدام concat بدلاً من الضغط

Description

البرمجة الوظيفية هي كل شيء عن إنشاء واستخدام وظائف غير متحولة. قدم التحدي الأخير طريقة concat كطريقة لدمج المصفوفات في صف جديد دون تحوير المصفوفات الأصلية. قارن concat إلى طريقة push . يضيف Push عنصرًا إلى نهاية الصفيف نفسه الذي يطلق عليه ، والذي يحول ذلك الصفيف. إليك مثال على ذلك:
var arr = [1، 2، 3]؛
arr.push ([4، 5، 6])؛
// arr يتم تغييرها إلى [1 ، 2 ، 3 ، [4 ، 5 ، 6]]
// ليس طريقة البرمجة الوظيفية
يوفر Concat طريقة لإضافة عناصر جديدة إلى نهاية صفيف بدون أي تأثيرات جانبية متحولة.

Instructions

تغيير الدالة nonMutatingPush بحيث يستخدم concat لإضافة newItem إلى نهاية original بدلاً من push . يجب على الدالة إرجاع صفيف.

Tests

tests:
  - text: يجب أن تستخدم التعليمات البرمجية الخاصة بك طريقة <code>concat</code> .
    testString: 'assert(code.match(/\.concat/g), "Your code should use the <code>concat</code> method.");'
  - text: يجب ألا تستخدم شفرتك طريقة <code>push</code> .
    testString: 'assert(!code.match(/\.push/g), "Your code should not use the <code>push</code> method.");'
  - text: يجب أن لا تتغير الصفيف <code>first</code> .
    testString: 'assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), "The <code>first</code> array should not change.");'
  - text: لا يجب تغيير الصفيف <code>second</code> .
    testString: 'assert(JSON.stringify(second) === JSON.stringify([4, 5]), "The <code>second</code> array should not change.");'
  - text: '<code>nonMutatingPush([1, 2, 3], [4, 5])</code> <code>[1, 2, 3, 4, 5]</code> .'
    testString: 'assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), "<code>nonMutatingPush([1, 2, 3], [4, 5])</code> should return <code>[1, 2, 3, 4, 5]</code>.");'

Challenge Seed

function nonMutatingPush(original, newItem) {
  // Add your code below this line
  return original.push(newItem);

  // Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingPush(first, second);

Solution

// solution required