-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgos
116 lines (93 loc) · 2.22 KB
/
Algos
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
/*Factorial via Recursion Begin*/
function fact (n){
if(n==1){
return n;
}else {
let returnValue = n* fact(n-1);
return returnValue;
}
}
console.log(fact(6));
/*Factorial via Recursion End*/
/*Adding element to the end of an Array*/
var array = [1,2,3,4,5];
array.push(6);
/*Adding elements to the begining of an Array*/
var array = [1,2,3,4,5];
array.unshift(6);//can accept multiple arguments
/*Removing elements from the end of an Array*/
var array = [1,2,3,4,5];
array.pop();
/*Removing element from the begining of an Array*/
var array = [1,2,3,4,5];
array.shift();
/*Adding elements to the middle of an Array*/
var array = [1,2,3,4,5];
array.splice(Math.floor((array.length-1)/2),0,10,11);
/*Removing elements from the middle of an Array*/
var array = [1,2,3,4,5];
array.splice(Math.floor((array.length-1)/2),2,10,11);
/*Ordering Alphabetic Array elements*/
var array = ["David","Mike","Cynthia","Clayton","Bryan","Raymond"];
array.sort();
/*Ordering Array of numbers*/
function test(a,b){
return a-b
}
var array = [10,9,8,13,4,5];
array.sort(test);
/*Builtin Array iterators*/
function test(a) {
console.log(a);
}
var nums = [1,2,3,4,5,6,7,8,9,10];
nums.forEach(test);
function test(a){
return a==0;
}
function retTest(a){
return a == b;
}
var zeros = nums.every(retTest);
var zeros = nums.some(retTest);
function add(a,b){
return a+b;
}
var array = [1,2,3,4,5,6,7,8,9,10];
var total = array.reduce(add);//reduceRight(function(){}); available as well
/*Mapping the array elements and have a new array*/
function map(a){
return a*a;
}
var in = [77, 65, 81, 92, 83];
var out = grades.map(map);
/*Filter the Array*/
var anarray = [1,2,4,5,6,7,8,9,10,11,12];
var isEven = (ele) =>{return !(ele%2);}
anarray.filter(isEven);
console.log(anarray);
/*List ADT implementation*/
function List(){
this.listSize = 0;
this.pos = 0;
this.dataStore = [];
this.clear = clear;
this.find = find;
this.toString = toString;
this.insert = insert;
this.append = append;
this.remove = remove;
this.front = front;
this.end = end;
this.prev = prev;
this.next = next;
this.length = length;
this.currPos = currPos;
this.moveTo = moveTo;
this.getElement = getElement;
this.length = length;
this.contains = contains;
function clear (){
console.log(this)
}
}