mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 17:50:39 +08:00
@ -6,9 +6,23 @@
|
||||
/**
|
||||
* Create a new string and append
|
||||
* @complexity O(n)
|
||||
*
|
||||
* Doctests
|
||||
*
|
||||
* > ReverseStringIterative('some')
|
||||
* 'emos'
|
||||
* > ReverseStringIterative('string')
|
||||
* 'gnirts'
|
||||
* > ReverseStringIterative('The Algorithms Javascript')
|
||||
* 'tpircsavaJ smhtiroglA ehT'
|
||||
* > ReverseStringIterative([])
|
||||
* ! TypeError
|
||||
* > ReverseStringIterative({})
|
||||
* ! TypeError
|
||||
* > ReverseStringIterative(null)
|
||||
* ! TypeError
|
||||
*/
|
||||
|
||||
function ReverseStringIterative (string) {
|
||||
function ReverseStringIterative(string) {
|
||||
if (typeof string !== 'string') {
|
||||
throw new TypeError('The given value is not a string')
|
||||
}
|
||||
@ -25,12 +39,26 @@ function ReverseStringIterative (string) {
|
||||
/**
|
||||
* JS disallows string mutation so we're actually a bit slower.
|
||||
*
|
||||
* @complexity: O(n)
|
||||
* @complexity O(n)
|
||||
*
|
||||
* 'some' -> 'eoms' -> 'emos'
|
||||
*
|
||||
* Doctests
|
||||
*
|
||||
* > ReverseStringIterativeInplace('some')
|
||||
* 'emos'
|
||||
* > ReverseStringIterativeInplace('string')
|
||||
* 'gnirts'
|
||||
* > ReverseStringIterativeInplace('The Algorithms Javascript')
|
||||
* 'tpircsavaJ smhtiroglA ehT'
|
||||
* > ReverseStringIterativeInplace([])
|
||||
* ! TypeError
|
||||
* > ReverseStringIterativeInplace({})
|
||||
* ! TypeError
|
||||
* > ReverseStringIterativeInplace(null)
|
||||
* ! TypeError
|
||||
*/
|
||||
|
||||
function ReverseStringIterativeInplace (string) {
|
||||
function ReverseStringIterativeInplace(string) {
|
||||
if (typeof string !== 'string') {
|
||||
throw new TypeError('The given value is not a string')
|
||||
}
|
||||
|
Reference in New Issue
Block a user