I have write my own solution for the given problem which is reverse a string

I have write my own solution for the given problem which is reverse a string
0

#1

My solution is not able to pass the test but I checked it is giving the desired output needed for that problem.Need Help.

Here is my code-

function reverseString(array) {
  var myString;
  var  newString=[]; 
  var value;
  var data;
  newString=array.reverse();
  value=newString.join("");
  data=('"'+value+'"');
  return data;
}



var string="hello";
var array=[];
array=string.split("");

reverseString(array);

#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.


#3

You should never change anything outside the function. So, splitting the string into an array before passing it as an argument to reverseString will not work. You can easily do this within the function:

function x(string){
  var array = string.split("");
}

What do you want to accomplish with this code:

And what is the difference between data and value?


#4

With this code I was concatenating double quotes with the output string like “hello”.Without this it was showing an error.For the data and value these are the variables which I was using and I have given them some random name.


#5

You won’t need it, because .join() already returns a string.

The way you do it, you are adding extra quotes to the string itself. Instead of olleh (string) you are returning "olleh" (also string), but you don’t need extra quotes around it.


#6

Thanks Ben for your help.