-
Notifications
You must be signed in to change notification settings - Fork 14
/
02.Array.js
117 lines (77 loc) · 2.94 KB
/
02.Array.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
/* * * * * * * * * * * * * * * * * * * * * *
* PALEO JS PT 2: ARRAY METHODS *
* * * * * * * * * * * * * * * * * * * * * */
// Part of what makes arrays so powerful are the many useful methods
// attached to them. They make it easy to take a set of data and `sort`
// or `filter` it. Note that normally these functions are called directly
// on an array, i.e. `[1, 2, 3].pop()`, but since we are doing this
// manually, we'll have to input the array as the first argument.
/** POP **/
// This removes the last element from an array and returns it.
// Since this is your first array method, I'll do it for you.
// Example Usage:
// var array = [6, 7, 8];
// array.pop(); --> returns 8, and array now equals [6, 7]
var pop = function(array) {
var last = array[array.length - 1];
if (array.length > 0) {
array.length--;
}
return last;
};
/** PUSH **/
// Just the opposite of `pop`, this adds new values to the end of an
// array, and returns the new length. Normally it can accept any number
// of values, but let's keep things simple for now and just do one.
// Example Usage:
// var array = [6, 7, 8];
// array.push(9); --> returns 4, and array now equals [6, 7, 8, 9]
var push = function(array, value) {
};
/** SHIFT **/
// Removes the first element from an array and returns it.
// Beware! This won't be as simple as `pop`.
// Example Usage:
// var array = [6, 7, 8];
// array.shift(); --> returns 6, and array now equals [7, 8]
var shift = function(array) {
};
/** UNSHIFT **/
// You may sense a pattern. This one adds any number of values to the
// start of an array. I let you off easy with push, but this time
// you'll need to use `arguments` and handle them all.
// Example Usage:
// var array = [6, 7, 8];
// array.unshift(3, 4, 5); --> returns 6, and array now equals [3, 4, 5, 6, 7, 8]
var unshift = function(array) {
};
/** JOIN **/
// Combines an array of sub-strings using a separator string inbetween
// each. If no separator is provided, it should default to a comma.
// Example Usage:
// ["hello", "world"].join(" "); --> returns "hello world"
var join = function(array, separator) {
};
/** REVERSE **/
// Reverses an array in place as well as returning the mutated array.
// To demonstrate reusing your functions, I've solved `reverse` in a
// bit of a odd way using your `push`... you got that working, right?
// Example Usage:
// var array = [3, 4, 5]
// array.reverse() --> returns [5, 4, 3] and array now equals [5, 4, 3]
var reverse = function(array) {
var reversed = [];
for (var i = array.length - 1; i >= 0; i--) {
push(reversed, array[i]);
}
for (var j = 0; j < array.length; j++) {
array[j] = reversed[j];
}
return array;
};
/** CONCAT **/
// This "concatenates" any number of sub-arrays into a new larger array.
// You may find previous functions useful here too!
// [1, 2].concat([3, 4], 5, [6]) --> returns [1, 2, 3, 4, 5, 6]
var concat = function() {
};