3.6 KiB
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(["challenge", "is", "not", "complete"])</code> يجب أن يعيد <code>["challenge", "complete"]</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