-
Notifications
You must be signed in to change notification settings - Fork 3
/
Exercise-Arrays-Answers.js
executable file
·118 lines (87 loc) · 1.95 KB
/
Exercise-Arrays-Answers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// ########## Exercise 01 ##########
var emptyArray = [];
emptyArray.length
Answer:
multiTypeArray = [0, 1, "two",
function () { return 3; },
{value1: 4, value2: 5}, [6, 7]];
console.log(multiTypeArray[0])
Answer:
console.log(multiTypeArray[2])
Answer:
console.log(multiTypeArray[3]())
Answer:
console.log(multiTypeArray[4].value1)
Answer:
console.log(multiTypeArray[4]["value2"])
Answer:
console.log(multiTypeArray[5][0])
Answer:
// ########## Exercise 02 ##########
var array = [];
array[0] = 1;
console.log(array)
Answer:
array[1] = 2;
console.log(array)
Answer:
array.push(3);
console.log(array)
Answer:
// ########## Exercise 03 ##########
var fourNumberArray = [1, 2, 3, 4];
console.log(fourNumberArray.length)
Answer:
fourNumberArray.push(5, 6);
console.log(fourNumberArray.length)
Answer:
// ########## Exercise 04 ##########
var array = ["peanut", "butter", "and", "jelly"];
console.log(array.slice(0, 1))
Answer:
console.log(array.slice(0, 2))
Answer:
console.log(array.slice(2, 2))
Answer:
console.log(slice(2, 20))
Answer:
console.log(array.slice(3, 0))
Answer:
console.log(array.slice(3, 100))
Answer:
console.log(array.slice(5, 1))
Answer:
// ########## Exercise 05 ##########
var array = [ "zero", "one", "two", "three", "four", "five" ];
function passedByReference(refArray) {
refArray[1] = "changed in function";
}
passedByReference(array);
console.log(array[1])
Answer:
var assignedArray = array;
assignedArray[5] = "changed in assignedArray";
console.log(array[5])
Answer:
var copyOfArray = array.slice()
copyOfArray[3] = "changed in copyOfArray";
console.log(array[3])
Answer:
var array = [1, 2];
array.push(3);
console.log(array)
Answer:
var poppedValue = array.pop();
console.log(poppedValue)
Answer:
console.log(array)
Answer:
var array = [1, 2];
array.unshift(3);
console.log(array)
Answer:
shiftedValue = array.shift();
console.log(shiftedValue)
Answer:
console.log(array)
Answer: