#142 #461 Adding Doctests to String/ReverseString.js

This commit is contained in:
Cristian Lupu
2020-10-30 18:24:34 +02:00
parent 2190b92e1b
commit 84e4c9906b

View File

@ -6,8 +6,22 @@
/** /**
* Create a new string and append * Create a new string and append
* @complexity O(n) * @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') { if (typeof string !== 'string') {
throw new TypeError('The given value is not a string') throw new TypeError('The given value is not a string')
@ -25,11 +39,25 @@ function ReverseStringIterative (string) {
/** /**
* JS disallows string mutation so we're actually a bit slower. * JS disallows string mutation so we're actually a bit slower.
* *
* @complexity: O(n) * @complexity O(n)
* *
* 'some' -> 'eoms' -> 'emos' * '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') { if (typeof string !== 'string') {
throw new TypeError('The given value is not a string') throw new TypeError('The given value is not a string')