2.3 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
56bbb991ad1ed5201cd392cc Manipulate Arrays With pop() 1 使用pop操作数组

Description

更改数组中数据的另一种方法是使用.pop()函数。 .pop()用于“弹出”数组末尾的值。我们可以通过将其赋值给变量来存储这个“弹出”值。换句话说, .pop()从数组中删除最后一个元素并返回该元素。任何类型的条目都可以从数组“弹出” - 数字,字符串,甚至嵌套数组。
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]

Instructions

使用.pop()函数从myArray删除最后一项,将“弹出”值分配给removedFromMyArray

Tests

tests:
  - text: '<code>myArray</code>应该只包含<code>[[&quot;John&quot;, 23]]</code> 。'
    testString: 'assert((function(d){if(d[0][0] == "John" && d[0][1] === 23 && d[1] == undefined){return true;}else{return false;}})(myArray), "<code>myArray</code> should only contain <code>[["John", 23]]</code>.");'
  - text: 在<code>myArray</code>上使用<code>pop()</code>
    testString: 'assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code), "Use <code>pop()</code> on <code>myArray</code>");'
  - text: '<code>removedFromMyArray</code>应该只包含<code>[&quot;cat&quot;, 2]</code> 。'
    testString: 'assert((function(d){if(d[0] == "cat" && d[1] === 2 && d[2] == undefined){return true;}else{return false;}})(removedFromMyArray), "<code>removedFromMyArray</code> should only contain <code>["cat", 2]</code>.");'

Challenge Seed

// Example
var ourArray = [1,2,3];
var removedFromOurArray = ourArray.pop();
// removedFromOurArray now equals 3, and ourArray now equals [1,2]

// Setup
var myArray = [["John", 23], ["cat", 2]];

// Only change code below this line.
var removedFromMyArray;

After Test

console.info('after the test');

Solution

// solution required