-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-algorithm-scripting.js
More file actions
144 lines (129 loc) · 3.51 KB
/
Copy pathbasic-algorithm-scripting.js
File metadata and controls
144 lines (129 loc) · 3.51 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
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
// Revers a String
function reverseString(str) {
return str.split('').reverse('').join('');
}
reverseString("hello");
// Factorialize a Number
function factorialize(num) {
var factor = 1;
for(var i = 1; i <= num; i++) {
factor *= i;
}
return factor;
}
factorialize(5);
// Check for Palindromes
function palindrome(str) {
var str1 = str.toLowerCase().replace(/[^A-Z0-9]/ig, '').split('').reverse().join('');
var str2 = str.toLowerCase().replace(/[^A-Z0-9]/ig, '');
return str1 === str2 ? true : false;
}
palindrome("eye");
// Find the Longest Word in a String
function findLongestWord(str) {
return str.split(' ').reduce( (max, item) => max < item.length ? item.length : max, 0);
}
findLongestWord("The quick brown fox jumped over the lazy dog");
// Title Case a Sentence
function titleCase(str) {
return str
.toLowerCase()
.split(' ')
.map(item => item.replace(item[0], item[0].toUpperCase()))
.join(' ');
}
titleCase("I'm a little tea pot");
// Return Largest Numbers in Arrays
function largestOfFour(arr) {
arr = arr.map( item => Math.max(...item));
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
// Confirm the Ending
function confirmEnding(str, target) {
var endOfStr = str.substr( str.length - target.length, target.length);
return endOfStr === target ? true : false;
}
confirmEnding("Bastian", "n");
// Repeat a string repeat a string
function repeatStringNumTimes(str, num) {
var answer = "";
for(var i = 0; i< num; i++) {
answer += str;
}
return answer;
}
repeatStringNumTimes("abc", 3);
// Truncate a string
function truncateString(str, num) {
if(num <= 3) {
return str.slice(0, num).concat('...');
}
if(str.length <= num) {
return str;
}
return str.slice(0, num-3).concat('...');
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);
// Chunky Monkey
function chunkArrayInGroups(arr, size) {
var newArr = [];
for(var i = 0; i < arr.length; i += size) {
newArr.push(arr.slice(i, size + i));
}
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d", "e"], 2);
// Slasher Flick
function slasher(arr, howMany) {
return arr.slice(howMany, arr.length);
}
slasher([1, 2, 3], 2);
// Mutations
function mutation(arr) {
var firstWord = arr[0].toLowerCase().split('');
var secWord = arr[1].toLowerCase().split('');
for( var i = 0; i < secWord.length; i++) {
if (firstWord.indexOf(secWord[i]) === -1) {
return false;
}
}
return true;
}
mutation(["hello", "hey"]);
// Falsy Bouncer
function bouncer(arr) {
return arr.filter(item => Boolean(item));
}
bouncer([7, "ate", "", false, 9]);
// Seek and Destroy
function destroyer(arr) {
var args = Array.from(arguments).slice(1, arguments.length);
return arr.filter(item => args.indexOf(item) === -1);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
// Where do I belong
function getIndexToIns(arr, num) {
arr.push(num);
return arr.sort((a, b) => a - b).indexOf(num);
}
getIndexToIns([40, 60], 50);
// Caesars Cipher
function rot13(str) { // LBH QVQ VG!
function encode(character) {
var charCode = character.charCodeAt();
if(charCode >= 65 && charCode <= 90) {
return charCode + 13 > 90 ? (charCode % 26) + 65 : charCode + 13;
}
return character;
}
function decode(character) {
return typeof character !== 'number' ? character : String.fromCharCode(character);
}
return str
.split('')
.map(encode)
.map(decode)
.join('');
}
rot13("SERR PBQR PNZC");