-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-string.js
More file actions
28 lines (25 loc) · 805 Bytes
/
reverse-string.js
File metadata and controls
28 lines (25 loc) · 805 Bytes
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
/**
* Reverses a given string without using built-in array reversal methods.
*
* @param {string} input - The string to reverse.
* @returns {string} A new string with characters in reverse order.
*
* @example
* reverseString("hello");
* // → "olleh"
*/
function reverseString(input) {
let reversed = '';
// Iterate from the end of the string to the beginning,
// building a new string character by character.
for (let i = input.length - 1; i >= 0; i--) {
reversed += input[i];
}
return reversed;
}
// ---- Test calls (manual validation) ----
console.log(reverseString('hello')); // "olleh"
console.log(reverseString('JavaScript')); // "tpircSavaJ"
console.log(reverseString('A-')); // "-A"
console.log(reverseString('')); // ""
console.log(reverseString('12345')); // "54321"