Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Write a program to reverse string

 Reversing a string is one of the most frequently asked JavaScript questions in the technical round of interviews. Interviewers may ask you to write different ways to reverse a string, or they may ask you to reverse a string without using in-built methods, or they may even ask you to reverse a string using recursion.


There are potentially tens of different ways to do it, excluding the built-in reverse function, as JavaScript does not have one.


1. Reverse the string Without built-in functions in jS:


function reverseString(str) {
var reverseStr = "";
for (let i = str.length - 1; i >= 0; i--) {
reverseStr += str[i];
}
return reverseStr;
}
reverseString('hello'); Output: olleh We Can also write like that:
unction reverseString(str) {
var reverseStr = "";
for (let i = 0 I < str.length - 1; i++) {
reverseStr = str[I] + reverseStr;
}
return reverseStr;
}
reverseString('hello'); Output: olleh

Post a Comment

0 Comments