-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
116 lines (116 loc) · 2.99 KB
/
script.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
function reverseString(str) {
return str.split('').reverse().join('');
}
function factorialize(num) {
if(num === 0) { return 1;}
return num *= factorialize(num-1);
}
function palindrome(str) {
return str.toLowerCase().replace(/[\W_]/g,'') ===
str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('');
}
function findLongestWord(str) {
var arr = str.split(' ');
var max = arr[0].length;
for(var i = 1; i < arr.length; i++){
if(max < arr[i].length){
max = arr[i].length;
}
}
return max;
}
function findLongestWord_v2(str) {
return str.split(' ').
reduce(function(last,current){
return Math.max(last,current.length);
},0);
}
function titleCase(str) {
var arr = str.toLowerCase().split(' ');
var result = arr.map(function(item){
return item.replace(item.charAt(0),item.charAt(0).toUpperCase());
});
return result.join(' ');
}
function largestOfFour(arr) {
return arr.map(function(item){
return item.reduce(function(previos,current){
return (current > previos) ? current : previos;
});
});
}
function confirmEnding(str, target) {
return str.substr(-target.length) === target;
}
function repeatStringNumTimes(str, num) {
if(num < 1) { return '';}
if(num === 1) { return str;}
return str + repeatStringNumTimes(str, num-1);
}
function truncateString(str, num) {
if(str.length <= num) {return str;}
return str.substr(0,num > 3 ? num-3 : num)+'...';
}
function chunkArrayInGroups(arr, size) {
var array = [],i = 0;
while(i < arr.length){
array.push(arr.slice(i,i+size));
i += size;
}
return array;
}
function slasher(arr, howMany) {
return arr.splice(howMany);
}
function mutation(arr) {
for(var i = 0; i < arr[1].length; i++){
if(arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) === -1){
return false;
}
}
return true;
}
function mutation_v2(arr) {
return arr[1].toLowerCase().
split('').
every(function(item){
return arr[0].toLowerCase().
indexOf(item) !== -1;
});
}
function bouncer(arr) {
return arr.filter(Boolean);
}
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
return arr.filter(function(item){
for(var i = 1; i< args.length; i++){
if(item === args[i]) return false;
}
return true;
});
}
function getIndexToIns(arr, num) {
return arr.concat(num).sort(function(a,b){
return a > b;
}).indexOf(num);
}
function rot13(str) { // LBH QVQ VG!
return str.split('').map(function(item){
if( /[A-M]/.test(item)){
return String.fromCharCode(item.charCodeAt(0)+13);
}
else if( /[N-Z]/.test(item)){
return String.fromCharCode(item.charCodeAt(0)-13);
}
else { return item;}
}).join('');
}
function sumOfDigits(num){
var str = num.toString().split('').reduce(function(a,b){
return (+a)+(+b);
}).toString();
return { value : str,
length : str.length,
};
}