Reverse String Algorithm

I will test different solutions to reverse a string value in JavaScript.

Syntax of String substr()

string.substr(start, length)

Solution 1: String substr()

Iteration starts from 1 to lenght of string.

Solution 2: String substr()

Iteration starts from lenght of string to 0.

Syntax of String substring()

string.substring(start, end)

Solution 3: String substring()

Iteration starts from 0 to lenght of string.

Solution 4: String substring()

Iteration starts from lenght of string to 0.

Syntax of String charAt()

string.charAt(index)

Solution 5: String charAt()

Iteration starts from 0 to lenght of string.

Solution 6: String charAt()

Iteration starts from lenght of string to 0.

Syntax of String split(), Array reverse, Array join()

string.split(separator, limit)
array.reverse()
array.join(separator)

Solution 7: String split(), Array reverse(), Array join()

Syntax of Array reduceRight()

arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])

Solution 8: Array reduceRight()

Syntax of Array reduce()

arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue])

Solution 9: Array reduce()

Recursion

If the length of string is very long, then stack size will be problem.

Leave a Reply