-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.js
609 lines (444 loc) · 17.8 KB
/
exercises.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// A variety of javascript exercises to practice and demonstrate javascript knowledge. These problems came from whiteboard problems, freeCodeCamp, or other online resources.
// Destroyer Function
function destroyer(arr) {
// Remove all the values
//how many arguments do we have?
//iterate through arguments
//check against arr
for (let i = 1; i < arguments.length; i++) {
while (arr.indexOf(arguments[i]) !== -1) {
let index = arr.indexOf(arguments[i])
arr.splice(index, 1)
}
}
return arr;
}
// destroyer(newArr)
//-------------------------//
// Using the call method
//this refers to what is left of the dot
//nameObj has key "name"
var nameObj = {
name: 'hello'
}
var callName = function () {
// [nameObj].name
console.log(this.name)
}
callName.call(nameObj)
//-------------------------//
//Highest & Lowest Value (sorted)
// INPUT:
//single array with integer values
//no decimal places, assume positive value
// [1, 5, 42, 17, 12, 59]
// DESIRED OUTPUT:
// array containing highest and then lowest value
// [HighestInteger, LowestInteger]
var array = [1, 30, 10, 7, 243, 4]
function sortOut(arr) {
arr.sort((a, b) => {
// first iteration: a = 1, b=30
// a-b sorts in ascending order
return a - b
})
// return highest: end of the array,
// lowest: first item in array
return [arr[arr.length - 1], arr[0]]
}
sortOut(array)
// Results in [243, 1]
//-------------------------//
// Searching for Differences in Arrays
//INPUT: two arrays with positive, non-decimal integers
//DESIRED OUTPUT: array with numbers in first array NOT found in
//second array
function diffArray(arr1, arr2) {
var newArr = []
//iterate through one array
//looking for value in 2nd array
//if -1 because it's not found, add to newArr
arr1.forEach(e => {
if (arr2.indexOf(e) === -1) {
newArr.push(e)
}
})
return newArr
}
diffArray([1, 2, 3, 4, 5], [1, 3, 5])
//Resolves to [2, 4]
//-------------------------//
// Sum all numbers between two integers
//INPUT: Array with two positive, non-decimal integers [1, 5]
// DESIRED OUTPUT: sum integer of all combined integers between two integers
function sumAll(arr) {
//argument is array with two numbers
//iterate from one to the other
//adding values to final num
var sum = 0
let highest = arr[0] > arr[1] ? arr[0] : arr[1]
let lowest = arr[1] < arr[0] ? arr[1] : arr[0]
// determine where to start (lowest)
// and determine where to end (highest)
for (let i = lowest; i <= highest; i++) {
// iterate from lowest to highest adding that value
// to our final sum
sum += i
}
return sum
}
sumAll([5, 60])
//-------------------------//
//Change text input to be spinal case.
//INPUT: String only. Could contain spaces, underscores, and a variety of upper/lowercase letters.
//OUTPUT: Expect a string returned in spinal case. No spaces or underscores: (this-is-a-test)
function spinalCase(str) {
return str
.replace(/([a-z])([A-Z])/g, `$1 $2`)
//Looking for spaces (\s) and underscores (|_) to replace with a dash.
.replace(/([\s+|_])/g, `-`)
.toLowerCase()
}
//-------------------------//
//Find Longest Word
//INPUT: String with words sepearated by spaces. No underscores or dashes. Will always be string. Please do not count periods as part of a word's length. Example: "Find longest word here."
//OUTPUT: Integer with the length of the longest word, without period character. Example: 32
function findLongestWordLength(str) {
//Use a regular expression to replace/remove periods.
//Split words in string into an array.
//Set variable for keeping track of highest word length.
var stringArray = str.replace(/\./g, '').split(' ')
var greatestLength = 0
//Iterate through array, assinging length to variable if greater than previously stored (or 0) value.
stringArray.forEach((e => {
if (e.length > greatestLength) {
greatestLength = e.length
}
}))
return greatestLength
}
//Test Cases:
findLongestWordLength("The quick brown fox jumped over the lazy dogwatcher.")
//Should return 10.
findLongestWordLength("Worrysome.. Sentence is here to test.")
//Should return 9. Ignoring consecutive periods.
//-------------------------//
//Replacer Function: Replace 2nd arguement with 3rd arguement in the string (first arguement). If 2nd arguement is capital, make sure to capitalize 3rd arguement, too.
//INPUT: String with spaces. NO dashes or underscores (for all three arguements) Example: "The dog was nice", "dog", "cat"
//OUTPUT: String with correctly replace and capitalized/not-capitalized word passed in as 3rd arguement.
//Example: "The cat was nice"
function myReplace(str, before, after) {
//Assign first char in before to firstChar
//create capitalized instance of after variable
var firstChar = before.charAt(0)
var capitalAfter = after.charAt(0).toUpperCase() + after.slice(1)
//Return replaced string ternary based on whether or not the firstChar in before variable is capital or not.
//If it is capitalized, we need to replace before with the capitalized version of After.
//If not, we can just replace before directly with after
return firstChar === firstChar.toLowerCase() ?
str.replace(before, after) : str.replace(before, capitalAfter)
}
//Test Cases(provided):
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped")
//Accounting for a capitalized 2nd arguement, and lowercase 3rd arguement
myReplace("He is Sleeping on the couch", "Sleeping", "sitting")
//Should return "He is Sitting on the couch"
//-------------------------//
//DNA Sequence
//Return array of arrays holding the DNA sequence pairs when given half of the pair. So if given "C" return array with ["C", "G"].
//INPUT: String without spaces or extra characters. Might be lowercase. Might be uppercase. Example: "AtG"
//OUTPUT: Array of arrays holding each pair. Example: [["A", "T"], ["C", "G"]]
function pairElement(str) {
//Create array of characters
//Create empty array to push desired output
var stringArray = str.split('')
var finalArray = []
//Iterate through Array
//Use Switch/Case to account for 4 character conditions
stringArray.forEach((e => {
//Convert to uppercase to account for accidental lowercase entries
switch (e.toUpperCase()) {
case 'C':
finalArray.push(["C", "G"])
break;
case 'G':
finalArray.push(["G", "C"])
break;
case 'A':
finalArray.push(["A", "T"])
break;
case 'T':
finalArray.push(["T", "A"])
break;
//Default case for invalid characters
default:
console.log('Invalid entry given. Try again with any 4 characters: A, T, C, G')
}
}))
return finalArray
}
//-------------------------//
//Find the missing letter
//Given a string, find the missing letter from the alphabet
//INPUT: String without spaces or special characters. Already arranged in alphabetical order.
//OUTPUT: Missing letter, or if no missing letter, undefined
function fearNotLetter(str) {
//Create array from string arguement
//Create array for alphabet
var stringArray = str.toLowerCase().split('')
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("")
//find and return index in alphabet where the stringArray starts
var startIndex = alphabet.findIndex(((e => {
return e === str.toLowerCase().charAt(0)
})))
//iterate through the stringArray comparing its value at given index compared to alphabet at the startIndex
//increment startIndex per iteration (if more than one)
for (let i = 0; i < stringArray.length; i++) {
if (stringArray[i] !== alphabet[startIndex]) {
return alphabet[startIndex]
}
startIndex++
}
}
//Test Cases:
fearNotLetter("abce")
//Should return 'd'
fearNotLetter("stvwx")
//Should return 'u'
fearNotLetter("fghij")
//Should return undefined since no condition is met and nothing is returned
//-------------------------//
//uniteUnique function
//given an unknown number of arguments combine them into an array in the same order as provided [arg1, arg2, arg3] without any duplicates.
//INPUT: Unknown number of argumnets, but all arguments will be arrays of integers. No string values. No special cases to account for.
//OUTPUT: Single array with all unique integer values in order of arguments passed into the function.
function uniteUnique(arr) {
//Determine number of arguments
//create two empty Arrays, one to add all argument integer values, and one to push unique values to return.
var args = arguments.length
var concatArray = []
var finalArray = []
//iterate through arguments and concat them together
for (let i = 0; i < args; i++) {
concatArray = concatArray.concat(arguments[i])
}
//map over values and push unique values into finalArray
concatArray.map((e => {
finalArray.indexOf(e) > -1 ? '' : finalArray.push(e)
}))
return finalArray
}
//Test Cases(provied):
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])
//Should return: [1, 3, 2, 5, 4]
uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])
//Should return: [1, 2, 3, 5, 4, 6, 7, 8]
//-------------------------//
//Sum Fibonacci Sequence
function sumFibs(num) {
//Fibonacci sequence: Next value is sum of previous two integers
//INPUT: Given a positive integer. No strings or other values.
//OUTPUT: Sum of all odd values in a Fibonacci sequence up to (and including) the
//argument integer and return that single integer.
var currentValue = 1
var finalArray = [1, 1]
//Starting with a [1,1] base because any given positive integer will at least be 1.
//Less code to hard code a starting point that all values will meet the condition.
//You could account for this with logic instead of hard coding a start value, though.
while (currentValue <= num) {
//Iterate through while our latest Fibonacci number is <= given argument
//assign currentValue based on new array length.
currentValue = finalArray[finalArray.length - 1] + finalArray[finalArray.length - 2]
if (currentValue <= num) {
//If it is the same value or less than argument, we want to add to array.
finalArray.push(currentValue)
}
else {
//If not, we have an array with all the conditional values required.
//Instead of assigning to a variable, we can just return out the value here.
//Return will end the function returning a single sum integer
//Filter out even values
//Curry a reduce method to add all the odd values together
return finalArray
.filter(e => {
return e % 2 !== 0
})
.reduce((a, b) => {
return a + b
})
}
}
}
//Test Cases (provided):
sumFibs(1)
//Should return a number
sumFibs(4)
//Should return 5
sumFibs(75024)
//Should return 60696
sumFibs(75025)
//Should return 135721
//-------------------------//
//Sum Primes Function
//Prime is a number only divisible by 1 & itself: 2, 3, 5, 7
//INPUT: A max value, positive integer. Example: 10
//OUTPUT: A sum of all prime numbers up to (and including) the max value (argument). Example: 28
function sumPrimes(num) {
//2 is first prime number. First number given will at least include this prime number. Condition can be written to account for INPUT: 1 if need be.
var currentValue = 2
var primeArray = []
var notPrimeArray = []
//Iterate through every number from 2 to max value (num)
while (currentValue <= num) {
//We do not care if it can divide by 1, or itself. So we write our for loop starting at 2 and ending 1 less than the currentValue. Otherwise we are performing extra iterations for no reason.
//Double loop is getting into some time complexity I'd rather avoid. I'll come back and look at optimizing this approach when I can.
for (let i = 2; i < currentValue; i++) {
if (currentValue % i === 0) {
//If at any point the remainder is zero, push into notPrimeArray
notPrimeArray.push(currentValue)
}
}
//Outside of for loop, if the notPrimeArray is empty, we know that the currentValue is prime. So we push it into our prime array.
if (notPrimeArray.length === 0) {
primeArray.push(currentValue)
}
//Increase current value and reset notPrimeArray to empty for next iteration.
currentValue++
notPrimeArray.length = 0
}
//Simple reduce function to get our sum from the final array
return primeArray.reduce((a, b) => {
return a + b
})
}
//-------------------------//
//Palindrome Checker
//INPUT: Given a string which could have lower/uppercase letters or special characters: (), whitespace, comma, period or '-'. Example: "Rac_ecar"
//OUTPUT: True or false boolean value.
function palindrome(str) {
//Removing ( with .replace() ) all accounted special characters with regular expression before making string all lowercase and assigning to variable.
var filterString = str.replace(/[\()\s_,.-]/g, "").toLowerCase()
//Compare string to itself split into an array, reversed and joined back together to verify if palindrome. Returns boolean based on comparison.
return filterString === filterString.split("").reverse().join("") ? true : false
}
console.log(
palindrome("0_0 (: /-\ :) 0-0")
)
function binarySearch(array, item) {
//Sort faster than linear time complexity. Achieve n logn time complexity by reducing our search by log2 each iteration.
//Set max to the array length (accounting for 0 index)
//Set min for the first item in the array
var max = array.length - 1
var min = 0
var guess
while (min <= max) {
//Guess is reassigned a value every iteration, which is why we need it inside our while loop
//Guess is the amount of items in the array divided by two. We're splitting like we do in a binary search to reduce our time complexity.
//Every iteration, if not found, we move our guess to reflect the side of the array that will hold the value. This function assumes that our input array is already sorted.
guess = Math.floor((max + min) / 2)
if (array[guess] === item) {
//If we found it, return it. We always check for this first.
return guess
}
else {
//is our Guess less than the item?
//10 < 15 = true
if (array[guess] < item) {
//if so, our new minimum boundary can be our guess + 1 (because our guess didn't hit as === in our first if condition). Now we are sorting from that new boundary (11) and up to the max (unchanged).
min = guess + 1
}
//is our Guess greater than the item?
// 5 > 2 = true
else {
//if so, our new max boundary can be our guess -1. Same reasoning as above for the + 1. Now we are sorting from our min (unchanged) to our maximum (4)
max = guess - 1
}
}
}
//If no condition is met, number is not found in the array.
return "Not Found"
}
//Drop Elements Function
//INPUT: Array with positive integers unsorted and a callback function.
//OUTPUT: Return an array with all values after the callback function returns true.
//Example: [1, 2, 3], function(n){return n > 1} would return [2, 3] when it hits true for the 1st element in the array (2).
function dropElements(arr, func) {
var array = []
arr.forEach((e, i) => {
//Iterate through array, if true, we will splice it at that index and assign that array to our function scoped array and return said array.
if (func(e) === true) {
array = arr.splice(i)
}
})
return array
}
//------Sliding Window-----//
function maxSum(arr, n) {
let maxSum = 0;
let tempSum = 0;
if (arr.length < n) return "Array length is insufficient"
for (let i = 0; i < n; i++) {
maxSum += arr[i]
}
tempSum = maxSum
for (let j = n; j <= arr.length - n + 1; j++) {
tempSum = tempSum + arr[j] - arr[j - n]
if (tempSum > maxSum) {
maxSum = tempSum
}
}
return maxSum
}
//------Sliding Window-----//
//Non-naive solution for finding indices where sum === target
var twoSum = function (nums, target) {
let store = {}
//O(n)
for (let i = 0; i < nums.length; i++) {
let c = nums[i]
if (!store[c]) store[c] = i
}
//O(n)
for (let i = 0; i < nums.length; i++) {
let remainder = target - nums[i]
if (store[remainder] && store[remainder] !== i) {
return [i, store[remainder]]
}
}
return 'No solution found'
};
//O(2n) => O(n) amortized
//-------------------------//
// Using Sieve of Eratosthenes for Prime Numbers //
function prim(n) {
let sieve = new Array(n).fill(true);
let primes = []
for (let i = 2; i < Math.sqrt(n); i++) {
if (sieve[i]) {
for (let j = Math.pow(i, 2); j < n; j += i) {
sieve[j] = false
}
}
}
sieve.forEach((e, i) => {
if (e && i > 1) primes.push(i)
})
return primes
}
//---------------------------//
//-------------------------//
module.exports = {
destroyer,
sortOut,
diffArray,
sumAll,
spinalCase,
findLongestWordLength,
myReplace,
pairElement,
fearNotLetter,
uniteUnique,
sumFibs,
sumPrimes
}
//-------------------------//