-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.js
More file actions
75 lines (72 loc) · 2.21 KB
/
chapter3.js
File metadata and controls
75 lines (72 loc) · 2.21 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
///////////////////////////// Push Front /////////////////////////////////////
/* Given array and an additional value, insert this value at the beginning of the array.
Do this without using any built-in array.*/
function pushFront(arr, value){
temp = [value];
for(var i = 0; i < arr.length; i++){
temp[i+1] = arr[i];
}
arr = temp;
console.log(arr);
}
pushFront([1,4,6], 8);
///////////////////////////// Pop Front //////////////////////////////////////
/* Given array, remove and return the value at the beginning of the array.
Do this without using any built-in array methods except pop*/
function popFront(arr){
temp = [];
val = arr[0];
for(var i = 1; i < arr.length; i++){
temp[i-1] = arr[i];
}
arr = temp;
console.log(val);
}
popFront([2, 4, 67, 13]);
///////////////////////////// Insert At ///////////////////////////////////////
/* Given array, index, and additional value, insert the value into array at given
index. Do this without using built-in array methods. You can think of PushFront(arr, val)
as equivalent to InsertAt(arr, 0 , val)*/
function insertAt(arr, idx, value){
x = arr.length;
while(x > idx){
arr[x] = arr[x-1];
x--;
}
arr[x] = value;
return arr;
}
insertAt([2,4,6,10], 3, 8);
/////////////////////////// Remove At //////////////////////////////////////////
/* Given array and an index into array. remove and return the array value at that index.
Do this without using built-in array methods except pop(). Think of popFront(arr) as equivelant
to removeAt(arr, 0)*/
function removeAt(arr, idx){
temp = [];
x = 0;
for(var i = 0; i < arr.length; i++){
if(i != idx){
temp[x] = arr[i];
x++;
}
}
arr = temp;
return arr;
}
removeAt([2, 4, 30, 21], 2);
///////////////////////////// Double Trouble //////////////////////////////////
/* Create a function that changes a given array to list each original elements twice, retaining
original order. Convert [4, "Ulysses", 42, false] to [4,4 "Ulysses", "Ulysses", 42, 42, false, false].*/
function doubleTrouble(arr){
var x = 0;
var temp = [];
for(var i = 0; i < arr.length; i++){
temp[x] = arr[i];
x++;
temp[x] = arr[i];
x++;
}
arr = temp;
return arr;
}
doubleTrouble([2,4,6,8,11]);