-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
224 lines (184 loc) · 7 KB
/
index.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const convertHTML = require('./src/convertHtmlEntities');
const sumFibs = require('./src/sumAllOddFibonacciNumbers');
const sumPrimes = require('./src/sumAllPrimes');
const fearNotLetter = require('./src/missingLetter');
const smallestCommonsLib = require('./src/smallestCommonMultiple');
const dropElements = require('./src/dropIt');
const steamrollArray = require('./src/steamroller');
const binaryAgent = require('./src/binaryAgent');
const addTogether = require('./src/addTogether');
const orbitalPeriod = require('./src/orbitalPeriod');
// # JAVASCRIPT ALGORITHMS AND DATA STRUCTURES CERTIFICATIONS (300 hours)
// ## Basic JavaScript (110 Exercises)
// - Use Recursion to Create a Countdown
// e.g.
function countup(n) {
if (n < 1) {
return [];
} else {
const countArray = countup(n - 1);
countArray.push(n);
return countArray;
}
}
//console.log(countup(5)); // [1, 2, 3, 4, 5]
// - Function countdown:
function countdown(n) {
if (n < 1) {
return [];
} else {
const countArray = countdown(n - 1);
countArray.unshift(n);
return countArray;
}
}
//console.log("Function Countdown: " + countdown(5)); // [5, 4, 3, 2, 1]
// FIXME: Use Recursion to Create a Range of Numbers:
function rangeOfNumbers(startNum, endNum) {
if (startNum <= endNum) {
let result = rangeOfNumbers(startNum + 1, endNum);
result.unshift(startNum);
return result;
} else {
return [];
}
};
// console.log("Function rangeOfNumbers: " + rangeOfNumbers(1, 5));
// console.log("Function rangeOfNumbers: " + rangeOfNumbers(6, 9));
// console.log("Function rangeOfNumbers: " + rangeOfNumbers(4, 4));
// TODO: Repeat the exercises using: ternary operator, higer order functions like 'splice(0,0,n)', '[].concat(n-1)', '[n, ...function()]'
// ## ES6
// - Use Destructuring Assignment to Assign to Extract Values from Objects:
// Consider the following ES5 code:
const user = { name: 'John Doe', age: 34 };
const userName = user.name; // name = 'John Doe'
const userAge = user.age; // age = 34
// Here's an equivalent assignment statement using the ES6 destructuring syntax:
const { nameUser, ageUser } = user; // (!) nameUser and ageUser => undefined ???
// TODO: (node:3687) [DEP0016] DeprecationWarning: 'GLOBAL' is deprecated, use 'global'
// - Promise:
const makeServerRequest = new Promise((resolve, reject) => {
let responseFromServer = true;
if (responseFromServer) {
resolve("We got the data");
} else {
reject("Data not received");
}
});
makeServerRequest.then(result => {
// console.log(result);
});
makeServerRequest.catch(error => {
// console.log(error);
});
// ## Intermediate Algorithm Scripting.
// 6/21 Pig Latin:
function translatePigLatin(str) {
// Variables:
let regEx = /([aeiou])/i; // () matched results are included in the array. Use in split() method.
let vowelPosition;
let suffixOne = "ay";
let suffixTwo = "way";
let finalStr = "";
// Find vowel position:
vowelPosition = str.search(regEx);
// Case a: If word not contain vowel = str + "ay":
if (vowelPosition === -1) {
return str.concat(suffixOne);
} else if (vowelPosition === 0) {
return str.concat(suffixTwo);
} else {
let splitWord = str.split(regEx)
for (let index = 1; index < splitWord.length; index++) {
finalStr += splitWord[index];
}
finalStr += splitWord[0] + suffixOne;
return finalStr;
}
}
translatePigLatin("consonant");
translatePigLatin("california");
translatePigLatin("paragraphs");
translatePigLatin("glove");
translatePigLatin("algorithm");
translatePigLatin("eight");
//console.log(translatePigLatin("schwartz"));
//console.log(translatePigLatin("rhythm"));
// 7/21 Search and Replace:
function myReplace(str, before, after) {
let regEx = /^[A-Z]/;
if (before.search(regEx) === 0 && after.search(regEx) === -1) {
after = after.replace(/^\w/, firstLetter => firstLetter.toUpperCase());
}
return str.replace(before, after);
}
// console.log("1: " + myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped"));
// console.log("2: " + myReplace("Let us go to the store", "store", "male"));
// console.log("3: " + myReplace("He is Sleeping on the couch", "Sleeping", "sitting"));
// console.log("4: " + myReplace("This has a spellngi error", "spellngi", "spelling"));
// console.log("5: " + myReplace("His name is Tom", "Tom", "john"));
// console.log("6: " + myReplace("Let us get back to more Coding", "Coding", "algorithms"));
// 8/21 DNA Pairing
//console.log("\n *** 8/21 DNA Pairing *** \n");
function pairElement(str) {
// Variables:
let originalStrand = [];
let pairingStrand = [];
let dnaStrand = [];
// 1. Turn a String into an array of strings:
originalStrand = str.split('');
// 2. Create pairingStrand Array:
for (let i = 0; i < originalStrand.length; i++) {
switch (originalStrand[i]) {
case 'G':
pairingStrand.push("C");
break;
case 'C':
pairingStrand.push("G");
break;
case 'A':
pairingStrand.push("T");
break;
case 'T':
pairingStrand.push("A");
break;
default:
//console.log(`Sorry, there is no pairing element for ${originalStrand[i]}`);
break;
}
}
// 3. Create the definitive dnaStrand
for (let index = 0; index < originalStrand.length; index++) {
dnaStrand.push([originalStrand[index], pairingStrand[index]]);
}
return dnaStrand;
}
pairElement("GCG");
// #9/21 Missing letters:
console.log("Exercise #9/21 Missing letters: " + fearNotLetter("jkln"));
// Exercise #11/21 Convert HTML Entities:
convertHTML("Dolce & Gabbana");
// Exercise #12/21 Sum All Odd Fibonacci Numbers:
sumFibs(0);
// Exercise #13/21 Sum All Primes:
sumPrimes(10);
// Exercise #14/21 Smallest Common Multiple:
console.log("Exercise #14/21 Smallest Common Multiple: " + smallestCommonsLib.smallestCommons([23, 18]));
// Exercise #15/21 Drop it:
console.log("Exercise #15/21 Drop it: " + dropElements([1, 2, 3], function (n) {
return n < 3;
})
);
// Exercise #16/21 Steamroller
console.log("Exercise #16/21 Steamroller: " + steamrollArray([[["a"]],[["b"]]]));
// Exercise #17/21 Binary Agents:
console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
// Exercise #19/21 Arguments Optional
console.log(addTogether(2)(3));
// Exercise #21/21 Map the Debris - Orbital Period
console.log("\nExercise #21/21 Map the Debris - Orbital Period: ");
let orbitalObjects = [];
orbitalObjects = orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]);
orbitalObjects.forEach(object => {
console.log(object);
});