mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 01:55:51 +08:00
33 lines
758 B
JavaScript
33 lines
758 B
JavaScript
/**
|
|
* A short example showing how to reverse a string.
|
|
*/
|
|
function ReverseStringIterative (string) {
|
|
if (typeof string !== 'string') {
|
|
throw new TypeError('The given value is not a string')
|
|
}
|
|
let reversedString = ''
|
|
let index
|
|
|
|
for (index = string.length - 1; index >= 0; index--) {
|
|
reversedString += string[index]
|
|
}
|
|
|
|
return reversedString
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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
|
|
*/
|
|
|
|
const ReverseStringIterativeInplace = (str) => [...str].reverse().join('')
|
|
|
|
export { ReverseStringIterative, ReverseStringIterativeInplace }
|