|
1 | 1 | module("About Arrays (topics/about_arrays.js)"); |
2 | 2 |
|
| 3 | +//Question 40 asks about arrays where multiple values are stored in one variables// |
3 | 4 | test("array literal syntax and indexing", function() { |
4 | 5 | var favouriteThings = ["cellar door", 42, true]; // note that array elements do not have to be of the same type |
5 | | - equal(__, favouriteThings[0], 'what is in the first position of the array?'); |
6 | | - equal(__, favouriteThings[1], 'what is in the second position of the array?'); |
7 | | - equal(__, favouriteThings[2], 'what is in the third position of the array?'); |
| 6 | + equal("cellar door", favouriteThings[0], 'what is in the first position of the array?'); |
| 7 | + equal(42, favouriteThings[1], 'what is in the second position of the array?'); |
| 8 | + equal(true, favouriteThings[2], 'what is in the third position of the array?'); |
8 | 9 | }); |
9 | 10 |
|
| 11 | +//Question 41 asks what is the type of an array using the typeof function. An array is an [object]// |
10 | 12 | test("array type", function() { |
11 | | - equal(__, typeof([]), 'what is the type of an array?'); |
| 13 | + equal("object", typeof([]), 'what is the type of an array?'); |
12 | 14 | }); |
13 | 15 |
|
| 16 | +//Question 42 asks what the amount of values in the variable collection which is [3]// |
14 | 17 | test("length", function() { |
15 | 18 | var collection = ['a','b','c']; |
16 | | - equal(__, collection.length, 'what is the length of the collection array?'); |
| 19 | + equal(3, collection.length, 'what is the length of the collection array?'); |
17 | 20 | }); |
18 | 21 |
|
| 22 | +//Question 43 asks about the .splice function. This function adds or removes a value from original array by .splice('position of value', 'number of spots after to remove', 'added values') So varABC=(A, B, C) with a .splice(1,1) means to remove 1 item after the first position so output will be B, since B is removed. When accessing varABC again the output for that will now be A and C. So the var workingWeek is the spliced items Saturday and Sunday and new output for daysofWeek is Monday thru Friday. // |
19 | 23 | test("splice", function() { |
20 | 24 | var daysOfWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
21 | | - var workingWeek = daysOfWeek.splice(__, __); |
22 | | - ok(workingWeek.equalTo([__]), 'what is the value of workingWeek?'); |
23 | | - ok(daysOfWeek.equalTo([__]), 'what is the value of daysOfWeek?'); |
| 25 | + var workingWeek = daysOfWeek.splice(5, 2); |
| 26 | + ok(workingWeek.equalTo(['Saturday', 'Sunday']), 'what is the value of workingWeek?'); |
| 27 | + ok(daysOfWeek.equalTo(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']), 'what is the value of daysOfWeek?'); |
24 | 28 | }); |
25 | 29 |
|
| 30 | +//Question 44 talks about the .push and.pop functions. The push functions adds an item to the end of the array. The pop function removes an item starting from the end of the array.// |
26 | 31 | test("stack methods", function() { |
27 | 32 | var stack = []; |
28 | 33 | stack.push("first"); |
29 | 34 | stack.push("second"); |
30 | 35 |
|
31 | | - equal(__, stack.pop(), 'what will be the first value popped off the stack?'); |
32 | | - equal(__, stack.pop(), 'what will be the second value popped off the stack?'); |
| 36 | + equal("second", stack.pop(), 'what will be the first value popped off the stack?'); |
| 37 | + equal("first", stack.pop(), 'what will be the second value popped off the stack?'); |
33 | 38 | }); |
34 | 39 |
|
| 40 | +//Question 45 talks about unshift. They are just like .push but start from the beginning of the array. So in this example since unshift is done after the first two functions, whatever is the value will show up first in the array. Shift is the same as .pop but for the beginning of the array.// |
35 | 41 | test("queue methods", function() { |
36 | 42 | var queue = []; |
37 | 43 | queue.push("first"); |
38 | 44 | queue.push("second"); |
39 | 45 | queue.unshift("third"); |
40 | 46 |
|
41 | | - equal(__, queue.shift(), 'what will be shifted out first?'); |
42 | | - equal(__, queue.shift(), 'what will be shifted out second?'); |
| 47 | + equal("third", queue.shift(), 'what will be shifted out first?'); |
| 48 | + equal("first", queue.shift(), 'what will be shifted out second?'); |
43 | 49 | }); |
0 commit comments