-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSlessons.js
999 lines (718 loc) · 24.8 KB
/
JSlessons.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/* Basic Data Structures:
Iterate Through All an Array's Items Using For Loops
We have defined a function, filteredArray, which takes arr,
a nested array, and elem as arguments, and returns a new array.
elem represents an element that may or may not be present on one
or more of the arrays nested within arr. Modify the function,
using a for loop, to return a filtered version of the passed array
such that any array nested within arr containing elem has been removed. */
function filteredArray(arr, elem) {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf(elem) === -1) {
newArr.push(arr[i]);
};
};
console.log(newArr)
return newArr;
}
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));
//-------------------------------------------------
/*Basic Algorithm Scripting: Return Largest Numbers in Arrays
Return an array consisting of the largest number from each
provided sub-array. For simplicity, the provided array will
contain exactly 4 sub-arrays.*/
function largestOfFour(arr) {
const maxVal = [];
arr.forEach(function(subArr) {
maxVal.push(Math.max(...subArr));
});
return maxVal;
};
//-------------------------------------------------
/*
Check if a string (first argument, str)
ends with the given target string (second argument, target).
This challenge can be solved with the .endsWith() method,
which was introduced in ES2015. But for the purpose of
this challenge, we would like you to use one of the JavaScript
substring methods instead.
*/
function confirmEnding(str, target) {
str.slice( str.length - target.length ) === target ? str = true: str = false;
return str;
}
//-------------------------------------------------
/*
Basic Algorithm Scripting: Truncate a String
Truncate a string (first argument) if it is longer
than the given maximum string length (second argument).
Return the truncated string with a ... ending.
*/
function truncateString(str, num) {
if (num >= str.length) {
return str
};
return str.slice(0 ,num) + "...";
}
truncateString("Peter Piper picked a peck of pickled peppers", 11);
//-------------------------------------------------
/*
Basic Algorithm Scripting: Finders Keepers
Create a function that looks through an array
(first argument) and returns the first element in
the array that passes a truth test (second argument).
If no element passes the test, return undefined.
*/
function findElement(arr, func) {
return arr[arr.map(func).indexOf(true)];
};
findElement([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; });
//-------------------------------------------------
/*
Basic Algorithm Scripting: Boo who
Check if a value is classified as a boolean primitive.
Return true or false.
Boolean primitives are true and false.
*/
function booWho(bool) {
return (typeof bool) === 'boolean';
}
booWho(null);
//-------------------------------------------------
/*
Title Case a Sentence
Return the provided string with the first letter of each
word capitalized. Make sure the rest of the word is in lower
case.
For the purpose of this exercise, you should also capitalize
connecting words like "the" and "of".
*/
function titleCase(str) {
let arr = str.toLowerCase().split(' ')
return arr.map(x => x.replace(x[0], x[0].toUpperCase())).join(" ")
};
titleCase("I'M a little tEa pOt");
//-------------------------------------------------
/*
Slice and Splice
You are given two arrays and an index.
Use the array methods slice and splice to copy each element
of the first array into the second array, in order.
Begin inserting elements at index n of the second array.
Return the resulting array. The input arrays should remain
the same after the function runs.
*/
function frankenSplice(arr1, arr2, n) {
// It's alive. It's alive!
let newa= [...arr2];
newa.splice(n, 0, ...arr1);
return newa;
}
//-------------------------------------------------
/*
Falsy Bouncer
Remove all falsy values from an array.
Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.
*/
// This could have been easily resolved by doing:
// return arr.filter(Boolean)
// FYI
function bouncer(arr) {
// Don't show a false ID to this bouncer.
for (let i = 0; i < arr.length; i++) {
if (!arr[i]) {
arr.splice(i, 1);
i -= 1;
};
};
return arr
}
bouncer([11, "ate", 7, 8, '', false, 9]);
//-------------------------------------------------
/*
Where do I Belong
Return the lowest index at which a value (second argument)
should be inserted into an array (first argument) once it has been sorted.
The returned value should be a number.
*/
function getIndexToIns(arr, num) {
arr.push(num);
arr.sort((a, b) => a - b);
console.log(arr);
return arr.indexOf(num);
console.log(arr.indexOf(num))
}
getIndexToIns([2, 20, 10], 19);
//-------------------------------------------------
/*
Chunky Monkey
Write a function that splits an array (first argument) into groups the
length of size (second argument) and returns them as a two-dimensional array.
*/
function chunkArrayInGroups(arr, size) {
let newArr = [];
for (let i = 0; i < arr.length; i += size) {
newArr.push(arr.slice(i, (i + size)))
};
return newArr;
};
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4);
//-------------------------------------------------
// INTERMEDIATE ALGORITHMS
//-------------------------------------------------
/*
Sum All Numbers in a Range
We'll pass you an array of two numbers. Return the sum
of those two numbers plus the sum of all the numbers
between them.
The lowest number will not always come first.
*/
function sumAll(arr) {
let newArr = arr.sort((x,y) => x-y)
let arrBetw = between(...newArr);
return arrBetw.reduce((x,y) => x + y )
}
function between(x, y) {
let betweenNum = [];
for (x; x <= y; x++) {
betweenNum.push(x);
}
return betweenNum;
}
sumAll([14, 10]);
//-------------------------------------------------
/*
Diff Two Arrays
Compare two arrays and return a new array with any items only
found in one of the two given arrays, but not both. In other
words, return the symmetric difference of the two arrays.
Note
You can return the array with its elements in any order.
*/
function diffArray(arr1, arr2) {
let newArr1 = arr1.filter(function(x) {
if (arr2.indexOf(x) === -1) {
return x
} //Finding unique value of 1st array
});
let newArr2 = arr2.filter(function(y) {
if (arr1.indexOf(y) === -1) {
return y
} //Finding unique value of 2nd array
});
return newArr1.concat(newArr2)
}
diffArray([1, 2, 3, 6, 5], [1, 2, 3, 4, 5]);
//-------------------------------------------------
/*
Seek and Destroy
You will be provided with an initial array (the first argument
in the destroyer function), followed by one or more arguments.
Remove all elements from the initial array that are of the same
value as these arguments.
*/
function destroyer(arr) {
let arg = arr.slice.call(arguments, 1)
return arr.filter(function(x) {
if (!arg.includes(x)) {
return x;
}
})
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
//-------------------------------------------------
/*
Wherefore art thou
Make a function that looks through an array of objects
(first argument) and returns an array of all objects that
have matching name and value pairs (second argument). Each
name and value pair of the source object has to be present
in the object from the collection if it is to be included in
the returned array.
For example, if the first argument is [{ first: "Romeo",
last: "Montague" }, { first: "Mercutio", last: null },
{ first: "Tybalt", last: "Capulet" }], and the second
argument is { last: "Capulet" }, then you must return
the third object from the array (the first argument),
because it contains the name and its value, that was
passed on as the second argument.
*/
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
let keys = Object.keys(source);
arr = collection.filter(function(obj) {
let colKey = Object.keys(obj)
for (let i = 0; i < keys.length; i++) {
if (colKey.indexOf(keys[i]) === -1) {
return false
}
if (obj[colKey[colKey.indexOf(keys[i])]] !== source[keys[keys.indexOf(keys[i])]]) {
return false
}
}
return obj
})
console.log(arr)
// Only change code above this line
return arr;
}
whatIsInAName([{"a": 1, "b": 2, "c": 3}], {"a": 1, "b": 9999, "c": 3});
//-------------------------------------------------
/*
Spinal Tap Case
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
*/
function spinalCase(str) {
let arr = [];
arr = str.replace(/([a-z])([A-Z])/g, '$1 $2') === str ? str.replace(/_/g, " ").split(" ") : str.replace(/([a-z])([A-Z])/g, '$1 $2').split(" ")
return arr.join("-").toLowerCase();
}
spinalCase("ThisIsSpinalTap");
//-------------------------------------------------
/*
Pig Latin
Translate the provided string to pig latin.
Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay".
If a word begins with a vowel you just add "way" to the end.
Input strings are guaranteed to be English words in all lowercase.
*/
function translatePigLatin(str) {
let vowelRegex = /[aeiou]/i;
if (str.search(vowelRegex) === -1) {
return str.concat("ay")
}
return str.search(vowelRegex) === 0 ? str.concat("way") : str.slice(str.search(vowelRegex)).concat(str.slice(0, str.search(vowelRegex)) + "ay")
}
//-------------------------------------------------
/*
Search and Replace
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the sentence to perform the search and replace on.
Second argument is the word that you will be replacing (before).
Third argument is what you will be replacing the second argument with (after).
Note
Preserve the case of the first character in the original word when you are replacing it.
For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog"
*/
function myReplace(str, before, after) {
let regex = new RegExp(before, "i")
let firstChar = str.charAt(str.indexOf(str.match(regex)))
if (firstChar === firstChar.toUpperCase()) {
after = after.charAt(0).toUpperCase().concat(after.slice(1))
}
return str.replace(regex, after)
}
myReplace("A quick brown fox Jumped over the lazy dog", "jumped", "leaped");
//-------------------------------------------------
/*
DNA Pairing
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
Base pairs are a pair of AT and CG. Match the missing element to the provided character.
Return the provided character as the first element in each array.
For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
*/
function pairElement(str) {
let pair = [...str];
console.log(pair)
const convert = {
G: ["G", "C"],
C: ["C", "G"],
T: ["T", "A"],
A: ["A", "T"]
};
return pair.map( (elem) => convert[elem]);
}
pairElement("GCG");
//-------------------------------------------------
/*
Missing letters
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
*/
function fearNotLetter(str) {
let arr = []
for (let i = 0 ; i < str.length; i++ ) {
arr.push(str.charCodeAt(i));
}
return arr.reduce((x,y) => {
if (x - y === - 2) {
return String.fromCharCode(x + 1);
}
return y;
})
}
//-------------------------------------------------
/*
Sorted Union
Write a function that takes two or more arrays and returns a new array
of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in
their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final
array should not be sorted in numerical order.
*/
function uniteUnique() {
let newArr =[];
for (let i = 0; i < arguments.length; i++) {
newArr.push(...arguments[i])
console.log(arguments[i])
}
let unique = [...new Set(newArr)]
return unique
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
// ---------------------
/*
Convert HTML Entities
Convert the characters &, <, >, " (double quote), and ' (apostrophe),
in a string to their corresponding HTML entities.
*/
function convertHTML(str) {
// :)
let regex = /<|>|"|'|&/g;
let refObj = {
"<": "<",
">": ">",
'"': """,
"'": "'",
"&": "&"
}
return regex[Symbol.replace](str, (val) => {
return refObj[val]
})
}
convertHTML("Dolce & Gabbana");
//-------------------------------------------------
/*
Sum All Odd Fibonacci Numbers
Given a positive integer num, return the sum of all odd Fibonacci numbers
that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional
number in the sequence is the sum of the two previous numbers. The first six
numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less
than or equal to 10 are 1, 1, 3, and 5.
*/
function sumFibs(num) {
let oddSum = 1;
let fibCount = [1];
for (let i = 1; i <= num; i += fibCount.shift()) {
console.log(i)
if (i % 2 !== 0) {
console.log(i)
oddSum += i;
}
fibCount.push(i)
}
return oddSum;
}
sumFibs(75024);
//-------------------------------------------------
/*
Drop it
Given the array arr, iterate through and remove each element
starting from the first element (the 0 index) until the function
func returns true when the iterated element is passed through it.
Then return the rest of the array once the condition is satisfied,
otherwise, arr should be returned as an empty array.
*/
function dropElements(arr, func) {
let newArr = arr;
let val;
// perform while loop as long as function is false and array length is not zero.
while (!func(val) && newArr.length != 0) {
// When function if false, remove first element of the array
if (!func(newArr[0])) {
newArr.shift()
}
// when function is true, set val to first element of array to end while loop.
if (func(newArr[0])) {
console.log(newArr[0])
val = newArr[0]
}
}
return newArr;
}
dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;});
//-------------------------------------------------
/*
Steamroller
Flatten a nested array. You must account for varying levels of nesting.
*/
function steamrollArray(arr) {
let flatArray =[];
for (let i = 0; i < arr.length; i++) {
if ( !Array.isArray(arr[i]) ) {
flatArray.push(arr[i]);
} if ( Array.isArray( arr[i] ) ) {
let nestedArr = steamrollArray(arr[i]);
for (let i= 0; i < nestedArr.length; i++) {
flatArray.push(nestedArr[i]);
}
}
}
return flatArray
}
steamrollArray([5, [[4, 5, 6]], 5]);
//-------------------------------------------------
/*
Binary Agents
Return an English translated sentence of the passed binary string.
*/
function binaryAgent(str) {
let arr = str.split(" ");
return arr.map(x => {
return String.fromCharCode(parseInt(x, 2))
}).join('')
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
//-------------------------------------------------
/*
Everything Be True
Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
In other words, you are given an array collection of objects. The predicate pre will be an object property and you need to return true
if its value is truthy. Otherwise, return false.
*/
function truthCheck(collection, pre) {
for (let prop in collection) {
console.log(prop)
if(!collection[prop][pre]) {
console.log(false)
return false;
}
}
console.log(true)
return true
}
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age");
//-------------------------------------------------
/*
Arguments Optional
Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.
*/
function addTogether() {
let arr = [...arguments]
for (let i = 0; i < arr.length; i++) {
if ((typeof arr[i]) !== "number") {
return undefined;
} else if (arr.length === 1) {
return function add(y){
if (typeof y !== "number") {
return undefined;
}
return y + arr[i];
};
}
}
return arr.reduce((x, y) => x + y)
}
//-------------------------------------------------
/*
Make a Person
Fill in the object constructor with the following methods below:
getFirstName() getLastName() getFullName() setFirstName(first) setLastName(last) setFullName(firstAndLast)
Run the tests to see the expected output for each method.
The methods that take an argument must accept only one argument and it has to be a string.
These methods must be the only available means of interacting with the object.
*/
let Person = function(firstAndLast) {
let arr = firstAndLast.split(' ')
this.getFullName = function() {
return arr.join(' ');
};
this.getFirstName = function() {
return arr[0];
};
this.getLastName = function() {
return arr[1];
};
this.setFullName = function(firstAndLast) {
let newFull = [];
newFull = firstAndLast.split(' ');
for (let i = 0; i < arr.length; i++) {
// This verifies if both the firstname and lastname from the new input are different
if (arr[0] !== newFull[0] && arr[1] === newFull[1]) {
// if only first name is different, it will only return the setFirstName function with the new firstname
return this.setFirstName(newFull[0])
} else if (arr[1] !== newFull[1] && arr[0] === newFull[0]) {
//if only the lastname is different, it will return the setLastname function with the new lastname
return this.setLastName(newFull[1])
} else {
arr = newFull
return arr.join(" ")
}
}
};
this.setFirstName = function(first) {
arr[0] = first;
return this.getFullName(arr);
};
this.setLastName = function(last) {
arr[1] = last;
return this.getFullName(arr);
}
}
var bob = new Person('Jose Ross');
console.log(bob.setFullName('Billy Tobar'))
console.log(bob.getFirstName())
console.log(bob.getLastName())
console.log(bob.getFullName())
//-------------------------------------------------
/*
Map the Debris
Return a new array that transforms the elements' average altitude into their orbital periods (in seconds).
The values should be rounded to the nearest whole number. The body being orbited is Earth.
*/
function orbitalPeriod(arr) {
let GM = 398600.4418;
let earthRadius = 6367.4447;
let result = [];
for (let el in arr) {
result.push({
name: arr[el].name,
orbitalPeriod: Math.round( 2 * Math.PI * Math.sqrt( Math.pow( ( arr[el].avgAlt + earthRadius ) , 3 ) / GM ) )
})
console.log(result)
}
return result;
}
orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
//-------------------------------------------------
// FINAL JS PROJECTS
//-------------------------------------------------
//-------------------------------------------------
/*
Palindrome Checker
Return true if the given string is a palindrome. Otherwise, return false.
A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
*/
function palindrome(str) {
let regex = /[^a-z|0-9]|\s/g;
let arr = [...str.toLowerCase().replace(regex, '')];
return arr.join('') === arr.reverse().join('') ? true : false;
}
palindrome("A man, a plan, a canal. Panama");
//-------------------------------------------------
/*
Roman Numeral Converter
Convert the given number into a roman numeral.
All roman numerals answers should be provided in upper-case.
*/
function convertToRoman(num) {
let decNum = num
const romArr = ['M', 'CM', 'DC', 'D', 'CD', 'C', 'XC', 'LX', 'L', 'XL', 'X', 'IX', 'VI', 'V', 'IV', 'I'];
const numArr = [1000, 900, 600, 500, 400, 100, 90, 60, 50, 40, 10, 9, 6, 5, 4, 1];
let result = '';
for (let i = 0; i < numArr.length; i++){
if ((decNum - numArr[i]) >= 0) {
result += romArr[i];
decNum -= numArr[i];
i = -1;
}
}
return result;
}
//-------------------------------------------------
/*
Caesars Cipher
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher.
In a shift cipher the meanings of the letters are shifted by some set amount.
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places.
Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
*/
function rot13(str) { // LBH QVQ VG!
let regex = /[A-Z]/g;
let rotArr = [...str];
return rotArr.map(x => {
let val = 0;
if (x.charCodeAt() < 78 && x.match(regex)) {
val = x.charCodeAt() + 13;
return String.fromCharCode(val)
} else if (x.charCodeAt() >= 78 && x.match(regex)) {
val = x.charCodeAt() - 13;
return String.fromCharCode(val);
}
return x;
}).join('')
};
rot13("SERR YBIR?");
//-------------------------------------------------
/*
Telephone Number Validator
Return true if the passed string looks like a valid US phone number.
The user may fill out the form field any way they choose as long as it has the format of a valid US number.
The following are examples of valid formats for US numbers (refer to the tests below for other variants):
555-555-5555
(555)555-5555
(555) 555-5555
555 555 5555
5555555555
1 555 555 5555
*/
function telephoneCheck(str) {
let regExPhNum = /^1?[" "]?\(?\d{3}\)?[-|" "]?\d{3}[-|" "]?\d{4}$/g;
if (str.match(/\(/) && str.match(/\)/) || !str.match(/\(/) && !str.match(/\)/) ) {
return regExPhNum.test(str)
}
return false;
}
telephoneCheck("555-555-5555");
//-------------------------------------------------
/*
Cash Register
Design a cash register drawer function checkCashRegister() that accepts purchase price as
the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid)
as the third argument.
cid is a 2D array listing available currency.
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or
if you cannot return the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change
if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in
highest to lowest order, as the value of the change key.
*/
function checkCashRegister(price, cash, cid) {
let ttlStart = 0, ttlEnd = 0, change = [], apCid = cid.reverse(), rem = cash-price;
let cashDen = {
"ONE HUNDRED": 100,
"TWENTY": 20,
"TEN": 10,
"FIVE": 5,
"ONE": 1,
"QUARTER": 0.25,
"DIME": 0.1,
"NICKEL": 0.05,
"PENNY": 0.01
}
apCid.map(x => {
ttlStart += x[1]
let unitsCid = (x[1] / cashDen[x[0]]).toFixed()
let unitsNeed = Math.floor((rem.toFixed(2) / cashDen[x[0]]).toFixed(2))
if ( unitsNeed >= 1 && unitsNeed >= unitsCid ) {
rem = rem.toFixed(2) - unitsCid*cashDen[x[0]];
ttlEnd += (unitsNeed-unitsCid)*cashDen[x[0]]
change.push([x[0], unitsCid*cashDen[x[0]]])
} else if ( unitsNeed >= 1 && unitsCid > unitsNeed ) {
rem = rem.toFixed(2) - unitsNeed*cashDen[x[0]];
ttlEnd += (unitsCid-unitsNeed)*cashDen[x[0]]
change.push([x[0], unitsNeed*cashDen[x[0]].toFixed(2)])
}
ttlEnd += x[1]
return false
});
console.log(ttlStart)
if (ttlStart < price-cash || rem > 0) {
return {status: "INSUFFICIENT_FUNDS", change: []}
}
if (ttlStart - (cash-price) === 0) {
return {status: "CLOSED", change: apCid.reverse()}
}
return {status: "OPEN", change: change}
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);