-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodewars.js
228 lines (144 loc) · 4.14 KB
/
codewars.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
function getMiddle(s) {
if (s.length % 2 === 0) {
return s.slice(s.length / 2 - 1, s.length / 2 + 1)
} else {
return s.slice(s.length / 2, s.length / 2 + 1)
}
}
const filter_list = (list) => list.filter((x) => typeof x === 'number')
const isSquare = (num) => Math.sqrt(num) % 1 === 0 ? true : false
function XO(str) {
let xCount = 0
let oCount = 0
str.split('').map((x) => x.toLowerCase() === 'x' ? xCount++ : x.toLowerCase() === 'o' ? oCount++ : null)
return xCount === oCount ? true : false
}
const findShort = (str) => str.split(' ').reduce((a, b) => a.length <= b.length ? a : b).length
function getSum(a, b) {
let sum = 0
for (i = Math.min(a, b); i <= Math.max(a, b); i++) {
sum += i
}
return sum
}
function sumTwoSmallestNumbers(numbers) {
numbers.sort((a, b) => a - b)
return numbers[0] + numbers[1]
}
function DNAStrand(dna){
let newArr = []
dna.split('').forEach((x) => {
if (x === 'A') {
newArr.push('T')
} else if (x === 'T') {
newArr.push('A')
} else if (x === 'G') {
newArr.push('C')
} else if (x === 'C') {
newArr.push('G')
} else {
newArr.push(x)
}
})
return newArr.join('')
}
function longest(s1, s2) {
newArr = []
s1.split('').forEach((x) => newArr.includes(x) ? null : newArr.push(x))
s2.split('').forEach((x) => newArr.includes(x) ? null : newArr.push(x))
return newArr.sort().join('')
}
function accum(s) {
let newArr = []
s.split('').map((x, i) => {
let str = x.toLowerCase().repeat(i + 1);
newArr.push(str.charAt(0).toUpperCase() + str.slice(1));
})
return newArr.join('-')
}
const friend = (friends) => friends.filter((name) => name.length === 4)
function openOrSenior(data){
let newArr = []
data.forEach((x) => {
if (x[0] > 54 && x[1] > 7) {
newArr.push('Senior')
} else {
newArr.push('Open')
}
})
return newArr
}
function findNextSquare(sq) {
if (Math.sqrt(sq) % 1 != 0) {
return -1
} else {
return (Math.sqrt(sq) + 1) * (Math.sqrt(sq) + 1)
}
}
var uniqueInOrder=function(iterable){
let newArr = []
if (Array.isArray(iterable)){
iterable.forEach((itm, ind) => {
itm === iterable[ind - 1] ? null : newArr.push(itm)
})
} else {
iterable.split('').forEach((itm, ind) => {
itm === iterable[ind - 1] ? null : newArr.push(itm)
})
}
return newArr
}
function createPhoneNumber(numbers){
return `(${numbers[0]}${numbers[1]}${numbers[2]}) ${numbers[3]}${numbers[4]}${numbers[5]}-${numbers[6]}${numbers[7]}${numbers[8]}${numbers[9]}`
}
function oddOrEven(array) {
if (array.length === 0) {
return 'even'
} else {
return array.reduce((acc, val) => acc + val, 0) % 2 === 0 ? 'even' : 'odd'
}
}
function smallEnough(a, limit){
a.sort((num1, num2) => num1 - num2)
return a[a.length - 1] <= limit ? true : false
}
function divisibleBy(numbers, divisor){
let newArr = []
numbers.forEach((x) => x % divisor === 0 ? newArr.push(x) : null)
return newArr
}
const getAverage = (marks) => Math.floor(marks.reduce((acc, val) => acc + val, 0) / marks.length)
function printerError(s) {
const validCodes = 'abcdefghijklm'
let errors = 0
s.split('').forEach((x) => !validCodes.includes(x) ? errors++ : null)
return `${errors}/${s.length}`
}
function isTriangle (a, b, c) {
if (a + b <= c) {
return false
} else if (b + c <= a) {
return false
} else if (a + c <= b) {
return false
} else {
return true
}
}
const solution = (str, ending) => str.slice(-Math.abs(ending.length)) === ending ? true : ending.length === 0 ? true : false
function reverseWords(str) {
let newArr = []
str.split(' ').forEach((x) => newArr.push(x.split('').reverse().join('')))
return newArr.join(' ')
}
var number=function(array){
if (array.length === 0) {
return []
} else {
return array.map((x, i) => `${i + 1}: ${x}`)
}
}
const minMax = (arr) => [Math.min(...arr), Math.max(...arr)]
const sortByLength = (arr) => arr.sort((a, b) => a.length - b.length)
const solution = (nums) => nums === null ? [] : nums.sort((a, b) => a - b)
const removeUrlAnchor = (url) => url.split('#')[0]