Skip to content

Commit b689970

Browse files
Merge pull request #19 from wakidurrahman/feat/reverse-a-string
feat(reverse-a-string): reverse a string
2 parents 8356bc2 + 381da7f commit b689970

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

javascript-today-magazine/note.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
JavaScript Today Magazine
33
short: JavaScriptTM
44
[JavaScriptTM] Bubble Sort
5-
5+
[DataStructuresAlgorithms]
66
17.09.2022: Some common algorithms implemented in JavaScript: Bubble Sort, Selection Sort, and Merge Sort.
77
1. Bubble Sort
88
2. Selection Sort
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Reverse a string. It's a common interview question, and it's quite simple.
3+
* However, if you really want to impress your interviewer, you'd be well-off to understand multiple solutions to the problem.
4+
* Rather than using built-in methods like ".reverse()" (which is fine, but you may be asked to neglect using it), 👍
5+
* demonstrate your ability to solve it in different ways.
6+
*
7+
* Here are three solutions to reversing a string in JavaScript.👏
8+
*
9+
*
10+
* Directions
11+
* Given a string, return a new string with the reversed order of characters
12+
*/
13+
14+
// Solution 1:
15+
16+
function reverseStringSolutionOne(str: string) {
17+
return str.split("").reverse().join("");
18+
}
19+
20+
// Solution 2:
21+
22+
function reverseStringSolutionTwo(str: string) {
23+
let reversed: string = "";
24+
for (let character of str) {
25+
reversed = character + reversed;
26+
}
27+
return reversed;
28+
}
29+
30+
// Solution 3:
31+
32+
function reverseStringSolutionThree(str: string) {
33+
return str.split("").reduce((rev, char) => char + rev, "");
34+
}

0 commit comments

Comments
 (0)