* fix(curriculum): tests quotes * fix(curriculum): fill seed-teardown * fix(curriculum): fix tests and remove unneeded seed-teardown
2.9 KiB
2.9 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
5a661e0f1068aca922b3ef17 | Access an Array's Contents Using Bracket Notation | 1 |
Description
let ourArray = ["a", "b", "c"];In an array, each array item has an index. This index doubles as the position of that item in the array, and how you reference it. However, it is important to note, that JavaScript arrays are zero-indexed, meaning that the first element of an array is actually at the zeroth position, not the first. In order to retrieve an element from an array we can enclose an index in brackets and append it to the end of an array, or more commonly, to a variable which references an array object. This is known as bracket notation. For example, if we want to retrieve the
"a"
from ourArray
and assign it to a variable, we can do so with the following code:
let ourVariable = ourArray[0];In addition to accessing the value associated with an index, you can also set an index to a value using the same notation:
// ourVariable equals "a"
ourArray[1] = "not b anymore";Using bracket notation, we have now reset the item at index 1 from
// ourArray now equals ["a", "not b anymore", "c"];
"b"
, to "not b anymore"
.
Instructions
1
) of myArray
to anything you want, besides "b"
.
Tests
tests:
- text: <code>myArray[0]</code> is equal to <code>"a"</code>
testString: assert.strictEqual(myArray[0], "a", '<code>myArray[0]</code> is equal to <code>"a"</code>');
- text: <code>myArray[1]</code> is no longer set to <code>"b"</code>
testString: assert.notStrictEqual(myArray[1], "b", '<code>myArray[1]</code> is no longer set to <code>"b"</code>');
- text: <code>myArray[2]</code> is equal to <code>"c"</code>
testString: assert.strictEqual(myArray[2], "c", '<code>myArray[2]</code> is equal to <code>"c"</code>');
- text: <code>myArray[3]</code> is equal to <code>"d"</code>
testString: assert.strictEqual(myArray[3], "d", '<code>myArray[3]</code> is equal to <code>"d"</code>');
Challenge Seed
let myArray = ["a", "b", "c", "d"];
// change code below this line
//change code above this line
console.log(myArray);
Solution
// solution required