-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
simple-fun-number-176-reverse-letter.js
111 lines (97 loc) · 2.67 KB
/
simple-fun-number-176-reverse-letter.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
function reverseLetter(str) {
// a place to store the reversed string
// iterate over all the letters of the input string in reverse
// check if the current letter is a letter
// if so, append to the reversed string
// return the reversed string
}
function isLetter(character) {
const code = character.charCodeAt();
const isUpperCaseLetter = code >= 65 && code <= 90;
const isLowerCaseLetter = code >= 97 && code <= 122;
return isUpperCaseLetter || isLowerCaseLetter;
}
function reverseLetter(str) {
// a place to store the reversed string
let reversed = '';
// iterate over all the letters of the input string in reverse
for (let i = str.length - 1; i >= 0; i--) {
// check if the current letter is a letter
if (isLetter(str[i])) {
// if so, append to the reversed string
reversed += str[i];
}
}
// return the reversed string
return reversed;
}
function reverseLetter(str) {
return str
.split('')
.reverse()
.filter((c) => c.match(/[a-zA-Z]/))
.join('');
}
function reverseLetter(str) {
return str
.split('')
.reverse()
.filter((c) => c.match(/[a-z]/i))
.join('');
}
function reverseLetter(str) {
return str
.replace(/[^a-z]/ig, '')
.split('')
.reverse()
.join('');
}
function reverseLetter(str) {
return str
.split('')
.reverse()
.filter((c) => /[a-zA-Z]/.test(c))
.join('');
}
function reverseLetter(str) {
return [...str]
.reverse()
.filter((c) => /[a-zA-Z]/.test(c))
.join('');
}
function reverseLetter(str) {
return [...str].reduce((reversed, letter) => {
if (letter.match(/[a-z]/i)) return letter + reversed;
return reversed;
}, '');
}
function reverseLetter(str) {
return [...str].reduceRight((reversed, letter) => {
if (letter.match(/[a-z]/i)) return reversed + letter;
return reversed;
}, '');
}
function reverseLetter(str) {
return [...str].reduce((reversed, letter) => letter.match(/[a-z]/i) ? letter + reversed : reversed, '');
}
function reverseLetter(str) {
return [...str].reduceRight((reversed, letter) => letter.match(/[a-z]/i) ? reversed + letter : reversed, '');
}
function reverseLetter(str) {
return Array.prototype.reduce.call(str, (reversed, letter) => {
if (letter.match(/[a-z]/i)) return letter + reversed;
return reversed;
}, '');
}
function reverseLetter(str) {
if (!str) return '';
let letter = str[0];
if (!letter.match(/[a-z]/i)) {
letter = '';
}
return reverseLetter(str.slice(1)) + letter;
}
console.log(reverseLetter('krishan'),'nahsirk');
console.log(reverseLetter('ultr53o?n'),'nortlu');
console.log(reverseLetter('ab23c'),'cba');
console.log(reverseLetter('krish21an'),'nahsirk');