forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseString.js
More file actions
Latest commit
32 lines (27 loc) · 765 Bytes
/
Copy pathReverseString.js
File metadata and controls
32 lines (27 loc) · 765 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
29
30
31
32
/**
* A short example showing how to reverse a string.
*/
functionReverseStringIterative(string){
if(typeofstring!=='string'){
thrownewTypeError('The given value is not a string')
}
letreversedString=''
letindex
for(index=string.length-1;index>=0;index--){
reversedString+=string[index]
}
returnreversedString
}
/**
*
* @author dev-madhurendra
* Reverses a number by converting it to a string.
*
* @param {string} str - The number to reverse.
* @returns {string} The reversed number.
*
* @example
* const reversed = reverseString("hello"); // Returns olleh
*/
constReverseStringIterativeInplace=(str)=>[...str].reverse().join('')
export{ReverseStringIterative,ReverseStringIterativeInplace}