-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-1_Reverse_a_String.js
More file actions
35 lines (27 loc) · 1.45 KB
/
Problem-1_Reverse_a_String.js
File metadata and controls
35 lines (27 loc) · 1.45 KB
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
// Problem-1_Reverse_a_String with Solution, Explanation, Verification, and Execution:
// To reverse a string, we can implement the Solution like below:
// -------------------------------------------------------------------
function reverseString(input) {
let reversed = '';
for (let i = input.length - 1; i >= 0; i--) {
reversed += input[i];
}
return reversed;
}
// Explanation:
// ---------------
// This function initializes an empty string to store the result. It then iterates backward through the input string using a for loop, starting from the last character (accessed via index input.length - 1) and decrementing the index until reaching the first character. Each character is appended to the result string. This approach builds the reversed string manually without using array methods, ensuring a straightforward and controlled reversal.
// Verification:
// ----------------
// function reverseString(hello) {
// let reversed = '';
// for (let i = hello.length - 1; i >= 0; i--) {
// reversed += hello[i];
// }
// return reversed;
// }
// console.log(reverseString("hello"));
// Execution of the above code block in IDE environment with node.js installed:
// ----------------------------------------------------------------------
// Input (In the directory in IDE environment with node.js installed): 'node Problem-1_Reverse_a_String.js'
// Output (In the directory in IDE environment with node.js installed): 'olleh'