Reversing a string using for loop in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in a string as the only argument. The function should construct a new reversed string based on the input string using the for a loop.

Example

Following is the code −

const str = 'this is the original string';
const reverseString = (str = '') => {
   let reverse = '';
   const { length: len } = str;
   for(let i = len - 1; i >= 0; i--){
      reverse += str[i];
   };
   return reverse;
};
console.log(reverseString(str));

Output

Following is the output on console −

gnirts lanigiro eht si siht
raja
Published on 11-Dec-2020 09:30:50
Advertisements