-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2023-11-25 Codewars 7 Kyu Fundamentals - Simple Fun 176 - Reverse Letter.js
125 lines (87 loc) · 2.47 KB
/
2023-11-25 Codewars 7 Kyu Fundamentals - Simple Fun 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// 11/25/23 Saturday Codewars 7 Kyu Fundamentals - Simple Fun #176: Reverse Letter
// https://www.codewars.com/kata/58b8c94b7df3f116eb00005b/train/javascript
/*
Task
Given a string str, reverse it and omit all non-alphabetic characters.
Example
For str = "krishan", the output should be "nahsirk".
For str = "ultr53o?n", the output should be "nortlu".
Input/Output
[input] string str
A string consists of lowercase latin letters, digits and symbols.
[output] a string
*/
// 6th attempt - working
function reverseLetter(str) {
let reverseString = '';
for(let i = str.length - 1; i >= 0; i--) {
if(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') {
reverseString += str[i];
}
}
return reverseString;
}
// 5th attempt - not working, returning string but not reversed
function reverseLetter(str) {
let reverseString = '';
let strSplit = str.split('');
strSplit.forEach(character => {
if(character >= 'a' && character <= 'z' || character >= 'A' && character <= 'Z') {
reverseString += character;
}
})
return reverseString;
}
// 4th attempt
function reverseLetter(str) {
const letters = [];
let splitString = str.split('');
splitString.forEach(character => {
if(typeof character === 'string') {
letters.push(character);
}
})
return letters
.reverse()
.join('');
}
// 3rd attempt - trying to use unshift to reverse order but didn't work, still adding symbols
function reverseLetter(str) {
const letters = [];
let splitString = str.split('');
splitString.forEach(character => {
if(typeof character === 'string') {
letters.unshift(character);
}
})
return letters
.join('');
}
// Not working: "expected 'n?o35rtlu' to equal 'nortlu'"
// 2nd attempt
function reverseLetter(str) {
const letters = [];
let splitString = str.split('');
splitString.forEach(character => {
if(typeof character === 'string') {
letters.push(character);
}
})
return letters
.join('');
}
// Not working: "expected 'krishan' to equal 'nahsirk'"
// 1st attempt
function reverseLetter(str) {
const letters = [];
str.forEach(character => {
if(typeof character === 'string') {
letters.push(character);
}
});
return letters
.join('');
}
/* =============
Other Solutions
============= */