push()
و unshift()
لهما unshift()
: pop()
و shift()
. كما قد تكون قد خمنت الآن ، بدلاً من إضافة ، يزيل pop()
عنصرًا من نهاية صفيف ، بينما يزيل shift()
عنصرًا من البداية. الاختلاف الرئيسي بين pop()
و shift()
وأبناء عمومتهم push()
و unshift()
، هو أن كلا الأسلوبين لا unshift()
معلمات ، وكل منهما يسمح فقط بتعديل مصفوفة بواسطة عنصر واحد في كل مرة. لنلقي نظرة: اسمحوا تحيات = [ماذا يكون ما يصل؟ ، 'مرحبا' ، 'انظر يا!'] ؛يمكننا أيضًا إرجاع قيمة العنصر الذي تمت إزالته باستخدام أي من الطريقتين التاليتين:
greetings.pop ()؛
// الآن تساوي ['ما الأمر؟' ، 'مرحبا']
greetings.shift ()؛
// الآن تساوي ['مرحبا']
دعونا برزت = تحيات. pop () ؛
// إرجاع "مرحبًا"
// تحيات الآن تساوي []
popShift
، والتي تأخذ مصفوفة كوسيطة وترجع مصفوفة جديدة. قم بتعديل الوظيفة ، باستخدام pop()
و shift()
، لإزالة العناصر الأولى والأخيرة لصفيف الوسيطة ، وتعيين العناصر التي تم إزالتها للمتغيرات الخاصة بها ، بحيث يحتوي الصفيف الذي تم إرجاعه على قيمها. popShift(["challenge", "is", "not", "complete"])
يجب أن يعيد ["challenge", "complete"]
'
testString: 'assert.deepEqual(popShift(["challenge", "is", "not", "complete"]), ["challenge", "complete"], "popShift(["challenge", "is", "not", "complete"])
should return ["challenge", "complete"]
");'
- text: يجب أن تستخدم وظيفة popShift
الأسلوب pop()
testString: 'assert.notStrictEqual(popShift.toString().search(/\.pop\(/), -1, "The popShift
function should utilize the pop()
method");'
- text: يجب أن تستخدم وظيفة popShift
طريقة shift()
testString: 'assert.notStrictEqual(popShift.toString().search(/\.shift\(/), -1, "The popShift
function should utilize the shift()
method");'
```