freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/access-array-data-with-inde...

2.2 KiB
Raw Blame History

id title challengeType guideUrl videoUrl localeTitle
56bbb991ad1ed5201cd392ca Access Array Data with Indexes 1 https://chinese.freecodecamp.org/guide/certificates/access-array-data-with-indexes 使用索引访问数组数据

Description

我们可以使用indexes访问数组内部的数据。数组索引使用字符串使用的相同括号表示法编写,但不是指定字符,而是指定数组中的条目。与字符串一样,数组使用从零开始的索引,因此数组中的第一个元素是元素0
var array = [50,60,70];
阵列[0]; //等于50
var data = array [1]; //等于60
注意
数组名称和方括号之间不应有任何空格,如array [0] 。尽管JavaScript能够正确处理但这可能会让其他程序员在阅读代码时感到困惑。

Instructions

创建一个名为myData的变量,并使用括号表示法将其设置为等于myArray的第一个值。

Tests

tests:
  - text: 变量<code>myData</code>应该等于<code>myArray</code>的第一个值。
    testString: 'assert((function(){if(typeof myArray !== "undefined" && typeof myData !== "undefined" && myArray[0] === myData){return true;}else{return false;}})(), "The variable <code>myData</code> should equal the first value of <code>myArray</code>.");'
  - text: 应使用括号表示法访问变量<code>myArray</code>的数据。
    testString: 'assert((function(){if(code.match(/\s*=\s*myArray\[0\]/g)){return true;}else{return false;}})(), "The data in variable <code>myArray</code> should be accessed using bracket notation.");'

Challenge Seed

// Example
var ourArray = [50,60,70];
var ourData = ourArray[0]; // equals 50

// Setup
var myArray = [50,60,70];

// Only change code below this line.

After Test

console.info('after the test');

Solution

// solution required