Write a program to reverse the string in javascript.
Example:
Input: "beingcodeexpert"
Output: "trepxeedocgnieb"
Approach 1: Using iterative method
JavaScript
var str='beingcodeexpert';
if(!str || str.length < 2 || typeof str!== 'string') { return 'Not valid'; }
const revArray = []; const length = str.length - 1;
for(let i = length; i >= 0; i--) { revArray.push(str[i]); }
console.log(revArray.join(''));
Approach 2: Using reverse method
JavaScript
var str='beingcodeexpert';var revArr=str.split('').reverse().join('');console.log(revArr);
No comments:
Post a Comment