Published: 07 Jul 2017 › Updated: 07 Jul 2017
Reverse a string in javascript | Test for a site im working on
Reverse a string in javascript
First we'll create a function called revereString
function reverseString(str){
return reversedString;
}
Inside the reverseString we will create a variable called reversedString and return it.
function reverseString(str){
var reversedString;
return reversedString;
}
Now let's set the reversedString variable to this:
function reverseString(str){
var reversedString = str.split("").reverse().join("");
return reversedString;
}
- str.split("") splits the string after every character and puts it into an array.
- .reverse("") reverses the array outputted by split().
- .join("") joins every array element into a string.
To test the program run this
var string = "Hello";
var output = reverseString(string);
console.log(output);
// Expected output "olleH"
Leave Reverse a string in javascript | Test for a site im working on to:
Read more #javascript posts
Best Posts From luxio
We have not curated any of luxio's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From luxio
- 120 BETA pack opening STEEMMONSTERS @luxio
- Bad life decisions with @luxio
- This is a BIG test
- Watch me struggle to learn high school maths
- The BEST productivity tools for maximum performance
- Pokemon x SteemMonsters A E S T H E T I C
- Steem Monsters and coding >:)
- A FREE gift in the dip! Bittrex PUMP monitor - LIL PRE-PUMP :) (OPEN SOURCE)
- Reverse a string in javascript | Test for a site im working on
- Steemit: Suck up central