alt-library
Version:
ALT library - algorithm and data structures
30 lines (27 loc) • 848 B
JavaScript
/**
* Given a string as an input, return a string with the reversed
* order of characters
* Example
* ===========================
* stringReverse('hello') => 'olleh'
* ===========================
* @param {string} inputString
* @return {string}
*/
var stringReverse = function (inputString) {
if (typeof inputString !== "string") {
throw new TypeError(
"stringReverse() expected a string, but got " +
Object.prototype.toString.call(inputString) +
" instead"
);
}
// turn inputString into and array using String.prototype.split()
// then call Array.prototype.reverse() method
// and join the array back using Array.prototype.join() into a string and return the reversed string
return inputString
.split("")
.reverse()
.join("");
};
export default stringReverse;