-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicDataTypesArray.js
54 lines (44 loc) · 1.96 KB
/
basicDataTypesArray.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
/*
In javascript, Array is one of basic data types. To define an empty array, you can use var arr=new Array() or var arr=[]
Array has an attribute: length, if there is an array named arr, using arr.length to know how many elements are contained in the array.
Each element in the array has an index, use arr[index] to get the value of element.
index always start from 0, so the first element of array is arr[0], the last element of array is arr[arr.length-1].
If we want to add new elements to the array, you can use the array method: push(). It can add an element to the end of the array. Instead, if we want to remove the last element of the array, you can use the array method: pop(). for example:
var arr=[1,2,3]; //define an array arr contains elements 1 2 3
arr.push(4); //add element 4 to arr
console.log(arr) //[1,2,3,4]
arr.pop(); //remove the last element from arr
console.log(arr) //[1,2,3]
Task
I've written five functions. Each function receives a parameter arr which is an array. Complete the functions using arr inside the function bodies.
1. getLength(arr) should return length of arr
2. getFirst(arr) should return the first element of arr
3. getLast(arr) should return the last element of arr
4. pushElement(arr) should push an element to arr, and then return arr
5. popElement(arr) should pop an element from arr, and then return arr
When you have finished the work, click "Run Tests" to see if your code is working properly.
In the end, click "Submit" to submit your code pass this kata.
*/
function getLength(arr) {
//return length of arr
return arr.length;
}
function getFirst(arr) {
//return the first element of arr
return arr.shift();
}
function getLast(arr) {
//return the last element of arr
return arr.pop();
}
function pushElement(arr) {
var el = 1;
//push el to arr
arr.push(el);
return arr;
}
function popElement(arr) {
//pop an element from arr
arr.pop();
return arr;
}