-1

Input

var str = "ABC PQR XYZ";

Output

var output = "ZYX RQP CBA";

Community
  • 1
  • 1
VandanaRathi
  • 27
  • 1
  • 4

3 Answers3

3

it's been answered before How do you reverse a string in place in JavaScript?

function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
  o += s[i];
return o;
}
bakaDev
  • 367
  • 2
  • 7
2

You can use reduce for that :

'ABC PQR XYZ'.split('').reduce((state, value) => {
    return value + state;
});
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
0

Arrays can be reversed:

 let ouput = "ABC".split``.reverse().join``;
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • 1
    Since you're using ES6, you can use the spread operator to avoid the split: [...'ABC PQR XYZ'].reverse().join`` – Rick Hitchcock Aug 24 '17 at 15:55
  • The instructions were to do it in place without using build in functions, though. But yes, split / reverse / join is the best solution if you're able to use all of the in-built tools. – Nmuta Jul 01 '21 at 11:20