freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../basic-data-structures/remove-items-from-an-array-...

3.6 KiB

id title challengeType videoUrl localeTitle
587d78b2367417b2b2512b0f Remove Items from an Array with pop() and shift() 1 إزالة العناصر من صفيف مع pop () و shift ()

Description

كل من push() و unshift() لهما unshift() : pop() و shift() . كما قد تكون قد خمنت الآن ، بدلاً من إضافة ، يزيل pop() عنصرًا من نهاية صفيف ، بينما يزيل shift() عنصرًا من البداية. الاختلاف الرئيسي بين pop() و shift() وأبناء عمومتهم push() و unshift() ، هو أن كلا الأسلوبين لا unshift() معلمات ، وكل منهما يسمح فقط بتعديل مصفوفة بواسطة عنصر واحد في كل مرة. لنلقي نظرة:
اسمحوا تحيات = [ماذا يكون ما يصل؟ ، 'مرحبا' ، 'انظر يا!'] ؛

greetings.pop ()؛
// الآن تساوي ['ما الأمر؟' ، 'مرحبا']

greetings.shift ()؛
// الآن تساوي ['مرحبا']
يمكننا أيضًا إرجاع قيمة العنصر الذي تمت إزالته باستخدام أي من الطريقتين التاليتين:
دعونا برزت = تحيات. pop () ؛
// إرجاع "مرحبًا"
// تحيات الآن تساوي []

Instructions

لقد حددنا وظيفة ، popShift ، والتي تأخذ مصفوفة كوسيطة وترجع مصفوفة جديدة. قم بتعديل الوظيفة ، باستخدام pop() و shift() ، لإزالة العناصر الأولى والأخيرة لصفيف الوسيطة ، وتعيين العناصر التي تم إزالتها للمتغيرات الخاصة بها ، بحيث يحتوي الصفيف الذي تم إرجاعه على قيمها.

Tests

tests:
  - text: '<code>popShift([&quot;challenge&quot;, &quot;is&quot;, &quot;not&quot;, &quot;complete&quot;])</code> يجب أن يعيد <code>[&quot;challenge&quot;, &quot;complete&quot;]</code>'
    testString: 'assert.deepEqual(popShift(["challenge", "is", "not", "complete"]), ["challenge", "complete"], "<code>popShift(["challenge", "is", "not", "complete"])</code> should return <code>["challenge", "complete"]</code>");'
  - text: يجب أن تستخدم وظيفة <code>popShift</code> الأسلوب <code>pop()</code>
    testString: 'assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1, "The <code>popShift</code> function should utilize the <code>pop()</code> method");'
  - text: يجب أن تستخدم وظيفة <code>popShift</code> طريقة <code>shift()</code>
    testString: 'assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1, "The <code>popShift</code> function should utilize the <code>shift()</code> method");'

Challenge Seed

function popShift(arr) {
  let popped; // change this line
  let shifted; // change this line
  return [shifted, popped];
}

// do not change code below this line
console.log(popShift(['challenge', 'is', 'not', 'complete']));

Solution

// solution required