1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Daniel [21]
3 years ago
12

Write a function named print_backward that accepts a String as its parameter and prints the characters in the opposite order. Fo

r example, a call of print_backward("hello there!") should print the following output:_______.
!ereht olleh
If the empty string is passed, no output should be produced.
Type your JavaScript solution code here:
________________________________
This is a function exercise. Write a JavaScript function as described. Do not write a complete program; just the function(s) above.
Computers and Technology
1 answer:
noname [10]3 years ago
3 0

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

You might be interested in
HELP 99PTS If Answered
Alborosie

You will have to do this as we are not you and we do not know local business/websites. Sorry we could not help.

3 0
3 years ago
Read 2 more answers
The int function can convert floating-point values to integers, and it performs rounding up/down as needed.
omeli [17]

Answer:

Thks is true the int function changes a float value to an integer and will round up or down by default.

5 0
2 years ago
What is the term for the era created by the digital revolution?
ollegr [7]
We are in the modern era and the information era
8 0
3 years ago
how do you set up a slide show to play continuously, advancing through all slides without requiring your interaction? A. Click S
Archy [21]

Answer:

B. Click Set Up, Slide Show, and then select the Loop continously until 'Esc' and use timings, if present options.

Explanation:

PowerPoint presentation is a program which was developed by Microsoft. It is a platform which is used to create business presentation or educational presentations, etc.

In order to play the slide show continuously without our interaction, we need to do the following steps :

1. In the powerpoint presentation, we have to click  "Slide Show" tab in the ribbon and then click on the "Set up slide show" on the Set Up bar.

2. The set up slide show dialogue box will pop up. In this we have to  check or tick on the "Loop continuously until 'Esc' " in the "Show Option" and then click 'OK'

3. Now in the "Slides" preview side panel, we have to click the 1st slide , then Press and hold the 'Shift' key and then click the last slide. This selects all the slides.

4. Now, in the transition tab, in the "Timing" group, we have or uncheck or untick the " On Mouse Click " and also check the " After"

6 0
3 years ago
Which of the following is equivalent to (p&gt;=q)? <br> i) P q iv) !p Where are genius people?:)
Elena L [17]

Answer:

Explanation:

This is unsolvable if you have no variable substitutes

4 0
2 years ago
Other questions:
  • Where do scanned documents go in windows 10?
    11·1 answer
  • Betty took her friends bowling. they rented 4 lane. 10 people need rent shoes and
    12·1 answer
  • If johanna wants to label the x- and y-axes, she should click Layout, then
    8·1 answer
  • Which of the following button should always be included when designing a navigation pictures escape contact FAQ
    5·2 answers
  • With a DUI charge on a driver’s record ______________.
    10·1 answer
  • What is the advantage of maintaining a list of keywords while creating a design blueprint?
    13·2 answers
  • Python: Bad input on line 8. What is the fix, please can someone tell me I’m desperate?
    10·1 answer
  • What is a simulation?
    5·2 answers
  • A company is developing a smart TV that connects to a wireless home network. Which technology can best help to establish this co
    11·2 answers
  • Write a java code to print Multiplication Table Till 20
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!