3.1 KiB
3.1 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d78b2367417b2b2512b0e | Add Items to an Array with push() and unshift() | 1 | 使用push()和unshift()将项添加到数组 |
Description
Array.push()
和Array.unshift()
。两种方法都将一个或多个元素作为参数,并将这些元素添加到调用该方法的数组中; push()
方法将元素添加到数组的末尾, unshift()
将元素添加到开头。考虑以下: 让二十三'='XXIII';
让romanNumerals = ['XXI','XXII'];
romanNumerals.unshift('XIX','XX');
//现在等于['XIX','XX','XXI','XXII']
romanNumerals.push(二十三);
//现在等于['XIX','XX','XXI','XXII','XXIII']请注意,我们也可以传递变量,这使我们可以更灵活地动态修改数组的数据。
Instructions
mixedNumbers
,我们将一个数组作为参数传递。修改函数使用push()
和unshift()
将'I', 2, 'three'
到数组的开头,将7, 'VIII', 9
到结尾,以便返回的数组包含数字的表示形式按顺序1-9。 Tests
tests:
- text: '<code>mixedNumbers(["IV", 5, "six"])</code>现在应该返回<code>["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]</code>'
testString: 'assert.deepEqual(mixedNumbers(["IV", 5, "six"]), ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9], "<code>mixedNumbers(["IV", 5, "six"])</code> should now return <code>["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]</code>");'
- text: <code>mixedNumbers</code>函数应该使用<code>push()</code>方法
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.push\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>push()</code> method");'
- text: <code>mixedNumbers</code>函数应该使用<code>unshift()</code>方法
testString: 'assert.notStrictEqual(mixedNumbers.toString().search(/\.unshift\(/), -1, "The <code>mixedNumbers</code> function should utilize the <code>unshift()</code> method");'
Challenge Seed
function mixedNumbers(arr) {
// change code below this line
// change code above this line
return arr;
}
// do not change code below this line
console.log(mixedNumbers(['IV', 5, 'six']));
Solution
// solution required