Quotes help make search much faster. Example: "Practice Makes Perfect"

Friday, December 7, 2012

LearnStreet Javascript Lesson 3 Exercise 8



Reversing a String
Now that we've covered some commonly used string helper functions, let's try to do something a little harder. We're going to give you a string and you're going to return the reverse of the string. For example, if we give you "gum", you will return "mug".

How do you do this? Remember that we can use the + operator to combine two strings together, and we can use the charAt(index) function to get the character at a given index.

The variable message contains a three letter word. What is the reverse of the string stored in message?

message
=> 'yeh'
var messageReversed = message.charAt(2)+message.charAt(1)+message.charAt(0)
messageReversed
=> 'hey'
Good job! That was a tedious way to reverse a string. We'll go over an easier way to do this using something called a loop in a later section.

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts