-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy path01-strings-tasks.js
332 lines (301 loc) · 8.27 KB
/
01-strings-tasks.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
'use strict';
/********************************************************************************************
* *
* Plese read the following tutorial before implementing tasks: *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String *
* *
********************************************************************************************/
/**
* Returns the result of concatenation of two strings.
*
* @param {string} value1
* @param {string} value2
* @return {string}
*
* @example
* 'aa', 'bb' => 'aabb'
* 'aa','' => 'aa'
* '', 'bb' => 'bb'
*/
function concatenateStrings(value1, value2) {
return value1.concat(value2)
}
/**
* Returns the length of given string.
*
* @param {string} value
* @return {number}
*
* @example
* 'aaaaa' => 5
* 'b' => 1
* '' => 0
*/
function getStringLength(value) {
return value.length
}
/**
* Returns the result of string template and given parameters firstName and lastName.
* Please do not use concatenation, use template string :
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
*
* @param {string} firstName
* @param {string} lastName
* @return {string}
*
* @example
* 'John','Doe' => 'Hello, John Doe!'
* 'Chuck','Norris' => 'Hello, Chuck Norris!'
*/
function getStringFromTemplate(firstName, lastName) {
return `Hello, ${firstName} ${lastName}!`
}
/**
* Extracts a name from template string 'Hello, First_Name Last_Name!'.
*
* @param {string} value
* @return {string}
*
* @example
* 'Hello, John Doe!' => 'John Doe'
* 'Hello, Chuck Norris!' => 'Chuck Norris'
*/
function extractNameFromTemplate(value) {
return new RegExp("Hello, (.+)!").exec(value)[1]
}
/**
* Returns a first char of the given string.
*
* @param {string} value
* @return {string}
*
* @example
* 'John Doe' => 'J'
* 'cat' => 'c'
*/
function getFirstChar(value) {
return value.substring(0,1)
}
/**
* Removes a leading and trailing whitespace characters from string.
*
* @param {string} value
* @return {string}
*
* @example
* ' Abracadabra' => 'Abracadabra'
* 'cat' => 'cat'
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(value) {
return value.trim()
}
/**
* Returns a string that repeated the specified number of times.
*
* @param {string} value
* @param {string} count
* @return {string}
*
* @example
* 'A', 5 => 'AAAAA'
* 'cat', 3 => 'catcatcat'
*/
function repeatString(value, count) {
return value.repeat(count)
}
/**
* Remove the first occurrence of string inside another string
*
* @param {string} str
* @param {string} value
* @return {string}
*
* @example
* 'To be or not to be', 'not' => 'To be or to be'
* 'I like legends', 'end' => 'I like legs',
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(str, value) {
return str.replace(value, '')
}
/**
* Remove the first and last angle brackets from tag string
*
* @param {string} str
* @return {string}
*
* @example
* '<div>' => 'div'
* '<span>' => 'span'
* '<a>' => 'a'
*/
function unbracketTag(str) {
return str.replace('<', '').replace('>', '')
}
/**
* Converts all characters of the specified string into the upper case
*
* @param {string} str
* @return {string}
*
* @example
* 'Thunderstruck' => 'THUNDERSTRUCK'
* 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
*/
function convertToUpperCase(str) {
return str.toUpperCase()
}
/**
* Extracts e-mails from single string with e-mails list delimeted by semicolons
*
* @param {string} str
* @return {array}
*
* @example
* '[email protected]' => ['[email protected]']
*/
function extractEmails(str) {
return str.split(';')
}
/**
* Returns the string representation of rectangle with specified width and height
* using pseudograhic chars
*
* @param {number} width
* @param {number} height
* @return {string}
*
* @example
*
* '┌────┐\n'+
* (6,4) => '│ │\n'+
* '│ │\n'+
* '└────┘\n'
*
* (2,2) => '┌┐\n'+
* '└┘\n'
*
* '┌──────────┐\n'+
* (12,3) => '│ │\n'+
* '└──────────┘\n'
*
*/
function getRectangleString(width, height) {
var horizontalChar = '─';
var verticalChar = '│';
var innerChar = ' ';
var breakChar = '\n';
var str = '';
for(var h = 1; h <= height; h++) {
switch (h) {
case 1:
str += '┌' + horizontalChar.repeat(width-2) + '┐' + breakChar
break
case height:
str += '└' + horizontalChar.repeat(width-2) + '┘' + breakChar
break
default:
str += verticalChar + innerChar.repeat(width-2) + verticalChar + breakChar
}
}
return str
}
/**
* Encode specified string with ROT13 cipher
* See details: https://en.wikipedia.org/wiki/ROT13
*
* @param {string} str
* @return {string}
*
* @example
*
* 'hello' => 'uryyb'
* 'Why did the chicken cross the road?' => 'Jul qvq gur puvpxra pebff gur ebnq?'
* 'Gb trg gb gur bgure fvqr!' => 'To get to the other side!'
* 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
*
*/
function encodeToRot13(str) {
var amount = 13
var output = ""
for (var i = 0; i < str.length; i++) {
var c = str[i]
if (c.match(/[a-z]/i)) {
var code = str.charCodeAt(i)
if (code >= 65 && code <= 90) {
c = String.fromCharCode(((code - 65 + amount) % 26) + 65)
}
else if (code >= 97 && code <= 122) {
c = String.fromCharCode(((code - 97 + amount) % 26) + 97)
}
}
output += c
}
return output
}
/**
* Returns true if the value is string; otherwise false.
* @param {string} value
* @return {boolean}
*
* @example
* isString() => false
* isString(null) => false
* isString([]) => false
* isString({}) => false
* isString('test') => true
* isString(new String('test')) => true
*/
function isString(value) {
return Object.prototype.toString.call(value) === "[object String]"
}
/**
* Returns playid card id.
*
* Playing cards inittial deck inclides the cards in the following order:
*
* 'A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣',
* 'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦',
* 'A♥','2♥','3♥','4♥','5♥','6♥','7♥','8♥','9♥','10♥','J♥','Q♥','K♥',
* 'A♠','2♠','3♠','4♠','5♠','6♠','7♠','8♠','9♠','10♠','J♠','Q♠','K♠'
*
* (see https://en.wikipedia.org/wiki/Standard_52-card_deck)
* Function returns the zero-based index of specified card in the initial deck above.
*
* @param {string} value
* @return {number}
*
* @example
* 'A♣' => 0
* '2♣' => 1
* '3♣' => 2
* ...
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(value) {
const colors = '♣♦♥♠',
nums = 'A234567891JQK';
let color = value[value.length - 1],
num = value[0];
return colors.indexOf(color) * 13 + nums.indexOf(num);
}
module.exports = {
concatenateStrings: concatenateStrings,
getStringLength: getStringLength,
getStringFromTemplate: getStringFromTemplate,
extractNameFromTemplate: extractNameFromTemplate,
getFirstChar: getFirstChar,
removeLeadingAndTrailingWhitespaces: removeLeadingAndTrailingWhitespaces,
repeatString: repeatString,
removeFirstOccurrences: removeFirstOccurrences,
unbracketTag: unbracketTag,
convertToUpperCase: convertToUpperCase,
extractEmails: extractEmails,
getRectangleString: getRectangleString,
encodeToRot13: encodeToRot13,
isString: isString,
getCardId: getCardId
};