Answer:
Here is the JavaScript code:
function print_backward(string) {
var revString = "";
for (var i = string.length - 1; i >= 0; i--) {
revString += string[i]; }
return revString; }
document.write(print_backward("hello there!"))
Explanation:
The function print_backward takes a string as a parameter.
Then it creates a new variable revString to hold a resultant string in opposite order.
The loop iterates through the characters of given string parameter in reverse and adds each character of the string to revString in opposite order.
Then the function returns revString which is the string in opposite order.
document.write(print_backward("hello there!")) This statement calls print_backward method passing a string hello there! to the method in order to print/display the characters of string in opposite order.
Lets say the string is hello
Then the loop works as follows:
The variable i is initialized to string.length - 1
string.length returns the length of the string
The length of string is 5 as there are 5 characters in string hello.
Hence string.length - 1 = 5 -1 = 4. So i=4
The loop checks if the value of i is greater than or equals to 0. This is true because i=4
Then the program enters the body of the loop which has a statement:
revString += string[i]; this works as:
revString = revString + string[i];
As i = 4 So
At first iteration
revString = revString + string[4];
This means the last character of string hello is added to revString. The last character of hello is 'o'
revString = 'o'
The statement in loop body continues to execute and stops when the value of i becomes less than 0.
The value of is decremented by 1 i.e. i-- So i = 3
At second iteration
revString = revString + string[3];
This means the second from last character of string hello is added to revString. The second last character of hello is 'l'
revString = 'ol'
The value of is decremented by 1 i.e. i-- So i = 2
At third iteration
revString = revString + string[2];
This means the third from last character of string hello is added to revString. The third last character of hello is 'l'
revString = 'oll'
The value of is decremented by 1 i.e. i-- So i = 1
At fourth iteration
revString = revString + string[1];
This means the fourth from last character of string hello is added to revString. The fourth last character of hello is 'e'
revString = 'olle'
The value of is decremented by 1 i.e. i-- So i = 0
At fifth iteration
revString = revString + string[0];
This means the fifth from last character of string hello is added to revString. The fifth last character of hello is 'e'
revString = 'olleh'
The value of is decremented by 1 i.e. i-- and the loop breaks because the i>=0 evaluates to false.
So the program moves to the next statement:
return revString;
revString = 'olleh' is returned and the statement document.write(print_backward("hello")) prints olleh on output screen.
In the given example the string is hello there! So this works same like the explained example and prints the following output on screen:
Output:
!ereht olleh