freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/manipulate-arrays-with-unsh...

1.7 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56bbb991ad1ed5201cd392ce Manipulate Arrays With unshift() 1 使用unshift操作数组

Description

不仅可以shift元件关闭的阵列的开头的,也可以unshift元素添加到数组的开始,即在阵列的前添加元素。 .unshift()工作方式与.push()完全相同,但是不是在数组的末尾添加元素, unshift()会在数组的开头添加元素。

Instructions

使用unshift()["Paul",35]添加到myArray变量的开头。

Tests

tests:
  - text: '<code>myArray</code>现在应该有[[“Paul”35][“dog”3]]。'
    testString: 'assert((function(d){if(typeof d[0] === "object" && d[0][0] == "Paul" && d[0][1] === 35 && d[1][0] != undefined && d[1][0] == "dog" && d[1][1] != undefined && d[1][1] == 3){return true;}else{return false;}})(myArray), "<code>myArray</code> should now have [["Paul", 35], ["dog", 3]].");'

Challenge Seed

// Example
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "J", "cat"]

// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();

// Only change code below this line.

After Test

console.info('after the test');

Solution

// solution required