-
Notifications
You must be signed in to change notification settings - Fork 0
/
JS_reverseString.js
159 lines (91 loc) · 3.5 KB
/
JS_reverseString.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
function reverseString_with_charAt(str) {
let newString = "";
for (let i = 0; i < str.length; i++){
newString = str.charAt(i) + newString;
}
return newString;
}
console.log(reverseString_with_charAt("hello"));
console.log(reverseString_with_charAt("Anita lava la tina"));
console.log(reverseString_with_charAt("Howdy"));
console.log(reverseString_with_charAt("Greetings from Earth"));
function reverseString_with_split(str) {
let splittedString = str.split("");
let newString = "";
for (let i = 0; i < str.length; i++){
newString = splittedString[i] + newString;
}
return newString;
}
console.log(reverseString_with_split("hello"));
console.log(reverseString_with_split("Anita lava la tina"));
console.log(reverseString_with_split("Howdy"));
console.log(reverseString_with_split("Greetings from Earth"));
function reverseString_with_reverse(str) {
let splittedString = str.split("");
let newString = splittedString.reverse();
return newString.join("");
}
console.log(reverseString_with_reverse("hello"));
console.log(reverseString_with_reverse("Anita lava la tina"));
console.log(reverseString_with_reverse("Howdy"));
console.log(reverseString_with_reverse("Greetings from Earth"));
function reverseString_chained(str) {
let newString = str.split("").reverse().join("");
return newString;
}
console.log(reverseString_chained("hello"));
console.log(reverseString_chained("Anita lava la tina"));
console.log(reverseString_chained("Howdy"));
console.log(reverseString_chained("Greetings from Earth"));
function reverseString_inv_for_loop(str) {
let newString = "";
for (let i = str.length-1; i >=0; i--){
newString += str[i];
}
return newString;
}
console.log(reverseString_inv_for_loop("hello"));
console.log(reverseString_inv_for_loop("Anita lava la tina"));
console.log(reverseString_inv_for_loop("Howdy"));
console.log(reverseString_inv_for_loop("Greetings from Earth"));
function reverseString_with_array_push(str) {
let newString = [];
for (let i = str.length-1; i >=0; i--){
newString.push(str[i]);
}
return newString.join("");
}
console.log(reverseString_with_array_push("hello"));
console.log(reverseString_with_array_push("Anita lava la tina"));
console.log(reverseString_with_array_push("Howdy"));
console.log(reverseString_with_array_push("Greetings from Earth"));
// .substr() returns characters in a string beginning at a specified location.
console.log("hello".substr(1)); // "ello"
// .charAt() returns a specified character from a string.
console.log("hello".charAt(0)); // "h"
function reverseString_recursive(str) {
// terminal case:
if (str === ""){
return "";
}
else {
return reverseString_recursive(str.substr(1)) + str.charAt(0);
}
}
console.log(reverseString_recursive("hello"));
console.log(reverseString_recursive("Anita lava la tina"));
console.log(reverseString_recursive("Howdy"));
console.log(reverseString_recursive("Greetings from Earth"));
/*
The depth of the recursion is equal to the length of the String.
This solution will be really slow if the String is very long and the stack size is of major concern.
*/
// REFORMULATED WITH TERNARY OPERATOR:
function reverseString_rec_Ternary(str) {
return (str === "") ? "": reverseString_rec_Ternary(str.substr(1)) + str.charAt(0);
}
console.log(reverseString_rec_Ternary("hello"));
console.log(reverseString_rec_Ternary("Anita lava la tina"));
console.log(reverseString_rec_Ternary("Howdy"));
console.log(reverseString_rec_Ternary("Greetings from Earth"));