-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray4.js
More file actions
53 lines (46 loc) · 1.86 KB
/
array4.js
File metadata and controls
53 lines (46 loc) · 1.86 KB
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
//PREP (parameters, returns, examples, pseudocode)
//Training JS #4: Basic data types--Array
// I've written five function, each function receives a parameter: arr(an array), you should code something with arr.
// 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.
//Given 5 different functions, I need to return what it says in each function.
//Will I ever get a string, or an empty array? or some other edge case that I need to be aware of?
//for first one, I need to return the length. Can you give me an example output?
// For 2nd one, first element. Can you give me an example output?
//3rd - get the last element, can you give me an example output?
//4th - push the variable to into the array.
// pop an element- remove it from the end of the array.
function getLength(arr){
//return length of arr
return arr.length
}
function getFirst(arr){
//return the first element of arr
return arr[0]
}
function getLast(arr){
//return the last element of arr
return arr[arr.length-1]
}
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
}
//returns
console.log(getLength([1,2,3]))//3);
console.log(getFirst([1,2,3]))//1);
console.log(getLast([1,2,3]))//3);
console.log(pushElement([1,2,3]))//length,4);
console.log(popElement([1,2,3]))//length,2);