-4

Wanted to make reverse string without using the loop I am getting an array

function reverseString(str) {
  return str
    .split("")
    .reverse();
}
console.log(reverseString('coder'));
Not A Bot
  • 2,282
  • 2
  • 15
  • 28
Coder Life
  • 27
  • 5

2 Answers2

2

.split() creates an Array; after doing .reverse() it's still an Array.

Use Array.prototype.join() to return a String:

function reverseString(str) {
  return str
    .split("")
    .reverse()
    .join("");
}
console.log(reverseString('coder'));
Roko C. Buljan
  • 180,066
  • 36
  • 283
  • 292
1

You forgot .join("") after reverse;

Shantun Parmar
  • 436
  • 2
  • 13