3.1 KiB
Raw Blame History

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([&quot;IV&quot;, 5, &quot;six&quot;])</code>现在应该返回<code>[&quot;I&quot;, 2, &quot;three&quot;, &quot;IV&quot;, 5, &quot;six&quot;, 7, &quot;VIII&quot;, 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