Home > other >  Reverse an array of string but not index number
Reverse an array of string but not index number

Time:11-06

let str=["ram","prerna","riya"];

str=str.join(" ");

console.log(str.split("").reverse().join(""));

Output is - ayir anrerp mar which is wrong

But I want to print mar anrerp ayir

CodePudding user response:

Why not add .reverse() on the second line

let str=["ram","prerna","riya"];
str=str.reverse().join(" ")

console.log(str.split("").reverse().join(""))

CodePudding user response:

You can simply achieve it like this :

let str= ["ram", "prerna", "riya"];

const reverseResult = str.map(item => item.split("").reverse().join(""));

console.log(reverseResult.join(" "));

  • Related