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
What mistake might you make related to changing the data in a cell that's used in a formula?
scoundrel [369]

Answer:

ojalat sirve

Explanation:

Cuando en una fórmula, en vez de indicar una celda, fila o columna estamos enlazando una hoja de cálculo o libro, debemos hacerlo con comilla simple. Si empleamos otro signo o símbolo separador, la fórmula no entenderá correctamente lo que le estamos indicando.

6 0
3 years ago
______ is a certification program that recognizes sustainable building practices and strategies. Question 1 options: A) Brundtla
Pani-rosa [81]

Answer:

(C) LEED

Explanation:

LEED certification is a recognized worldwide as symbol of viability achievement.

(Leadership in Energy and Environmental Design(LEED):- It is the most popular and widely used rating system in the world for green buildings. Available for almost all building project types, including new constructions to interior fit-outs.

8 0
3 years ago
Suppose that the tuition for a university is $10,000 this year and increases 4% every year. In one year, the tuition will be $10
seropon [69]

Answer:

<em>The programming language is not stated; however, I'll answer using Python programming language (</em><em>Se</em><em>e attachment</em><em> </em><em>for</em><em> </em><em>proper </em><em>for</em><em>mat</em><em>)</em>

tuition = 10000

rate = 0.04

for i in range(1,15):

tuition = tuition + tuition * rate

if i <= 10:

print("Year "+str(i)+" tuition:",end=" ")

print(round(tuition,2))

if i == 14:

print("Tuition 4th year after:",end=" ")

print(round(tuition,2))

Explanation:

<em>The first 2 lines initializes tuition and rate to 10000 and 0.04 respectively</em>

tuition = 10000

rate = 0.04

<em>The next line iterates from year 1 to year 14</em>

for i in range(1,15):

<em>This line calculates the tuition for each year</em>

tuition = tuition + tuition * rate

<em>The next 3 lines prints the tuition for year 1 to year 10</em>

if i <= 10:

print("Year "+str(i)+" tuition:",end=" ")

print(round(tuition,2))

<em>The next 3 lines prints the tuition at the 4th year after year 10 (i.e. year 14)</em>

if i == 14:

print("Tuition 4th year after:",end=" ")

print(round(tuition,2))

7 0
3 years ago
The output for the following code will be: Pasta (1 mark)
Anastasy [175]

Answer:

The output is "Pasta"

Explanation:

Given

The attached code segment

Required

The output

The first line of the program implies that:

MyFavFood="Pasta"

This means that, the value of the variable MyFavFood is string "Pasta"

Next,

print (MyFavFood)

This prints the value of the variable MyFavFood which is "Pasta"

<em>Hence, the output is "Pasta"</em>

7 0
2 years ago
Alison is entering text in a Word document. As she is entering data, where will the cursor move in relation to the text she is t
Vinil7 [7]

The cursor should move right but if not give me more information to correct my statement

6 0
3 years ago
Other questions:
  • ________ is typically used as the last string of letters in the name of a website. Group of answer choices the name of the indiv
    13·1 answer
  • A commonly used font style is _____. superscript periwinkle times new roman point
    7·1 answer
  • Is this a Bad Cpu processor ? I need some.help ASAP I turn on my.pc and it has no display but everything is on fans and the you
    13·1 answer
  • Effective display designs must provide all the necessary data in the proper sequence to carry out the task. Identify a recent pe
    12·1 answer
  • What are the advantages and disadvantages of fortran?
    13·1 answer
  • what term defines inventiveness uncertainty and futuristic ideas typically deals with science and technology
    15·1 answer
  • In needs analysis: Group of answer choices the costs of different physical network design alternatives are assessed the rate of
    10·1 answer
  • Which of the following is not a bus type A. Address bus B. Data bus C. Memory bus D. Control bus ​
    7·2 answers
  • Write the function evens which takes in a queue by reference and changes it to only contain the even elements. That is, if the q
    6·1 answer
  • Your network uses a network address of 137. 65. 0. 0 with a subnet mask of 255. 255. 0. 0. How many ip addresses are available t
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!