-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy patharray-functions.js
More file actions
119 lines (98 loc) · 3.29 KB
/
array-functions.js
File metadata and controls
119 lines (98 loc) · 3.29 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
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
118
119
//in the function map, create a new array and store in a variable
//loop theArray and call the fnc for each thing in the array,
// passing in the item from the current loop into the call to fnc
//add the returned value from fnc to the new array
//return the new array
export function map(theArray, fnc){
const newArr = [];
for (let i=0; i<theArray.length; i++) {
newArr.push(fnc(theArray[i]));
}
return newArr;
}
//create a new array
//loop theArray and call the fnc for each thing in the array,
// passing in the item from the current loop
//fnc will return true or false, if true add the item to the new array else do not
//return the new array
export function filter(theArray, fnc){
const newArr = [];
for (let i=0; i<theArray.length; i++) {
if (fnc(theArray[i])) {
newArr.push(theArray[i]);
}
}
return newArr;
}
//loop theArray and call the fnc for each thing in the array,
// passing in the item from the current loop
//fnc will return true or false, if true return the item
//return null
export function find(theArray, fnc){
let foundElement = null;
for (let i=0; i<theArray.length; i++) {
if (fnc(theArray[i])) {
foundElement = theArray[i];
}
}
return foundElement;
}
//return the last item in theArray
export function findLast(theArray){
return theArray[theArray.length-1];
}
//return the first element of the array
export function head(theArray){
return theArray[0];
}
//create a new array
//loop theArray in reverse order
//add the item from each loop to the new array
//return the new array
export function reverse(theArray){
const newArr = [];
for (let i=theArray.length-1; i>=0; i--) {
newArr.push(theArray[i]);
}
return newArr;
}
//create a new array
//loop theArray
//add the item from each loop to the new array except the first item
//return the new array
export function tail(theArray){
const newArr = [];
for (let i=1; i<theArray.length; i++) {
newArr.push(theArray[i]);
}
return newArr;
}
//implement the most basic sorting algorithm there is
//assume the array will always have numbers
//use a while loop to constantly loop theArray until it is sorted
//use a for loop to loop theArray
//look at the current item and the next item, compare them
//if the items are out of order, swap them
//initialize a variable that indicates if a swap had to be done, set it to false
//if a swap is done set it to true
//after each for loop check the variable, if true, continue the while loop
//if false return theArray
export function sort(theArray){
let flip = false;
for (let i = 0; i< theArray.length; i++){
if (theArray[i] > theArray[i + 1]) {
flip = true;
// using the swap without temp algorithm! Ex: theArray[i] = 10, theArray[i+1] = 6
theArray[i+1] = theArray[i+1] - theArray[i]; //Ex. 6 - 10 = -4
theArray[i] = theArray[i] + theArray[i+1]; //Ex. 10 + (-4) = 6
theArray[i+1] = theArray[i] - theArray[i+1]; //Ex. 6 - (-4) = 10
}
}
// flip? (flip = false, return bubbleSort(theArray)) : return(theArray); // doesn't work.
if (flip) {
flip = false; // setting my check
return sort(theArray);
} else {
return theArray;
}
}