Files
JavaScript/String/ReverseString.js
Madhurendra Nath Tiwari 0ca18c2b2c fix: added reverse string inplace (#1406)
Co-authored-by: madhuredra <madhuredra.tiwari@zemosolabs.com>
2023-10-02 23:06:45 +05:30

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 }