Question

In this Javascript code, why are 2 for loops needed? could you please explain this to...

In this Javascript code, why are 2 for loops needed?

could you please explain this to me.

This is the outcome

*
**
***
****
*****
******
*******
********
*********

the code is

<html>
<head>
      
       <title></title>
      
<script>
      
      
   showStars(10);
      
       function showStars(rows){
           for(var i = 0; i < rows; i++){
               var pattern = "";
               for(var j = 0; j < i; j++)
                   pattern = pattern + "*";
           document.writeln(pattern);
           document.writeln("<br/>");
           }
       }
      
              
</script>
</head>

<body>
      
</body>
</html>

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The first for loop is for the row count.

And the second for loop is to generate the number of stars in that specific row.

Dry run the code:-

when i = 0

i = 0, j = 0

loop condition false as j == i, second loop exit. So loop did not generated any pattern.


when i =1

i =1, j = 0

pattern becomes "*"

i =1, j= 1

loop condition false as j == i, second loop exit

then the pattern = “*” is printed

and the line is changed.


when i = 2

i =2, j = 0

pattern = "*"

i =2, j = 1

pattern = "**"

i =2, j = 2

loop condition false as j == i, second loop exit

printed pattern "**"

changed line


when i = 3

i =3, j = 0

pattern = "*"

i =3, j = 1

pattern = "**"

i =3, j = 2

pattern = "***"

i =3, j = 3

loop condition false as j == i, second loop exit

printed pattern "***"

changed line

and so on...


The first(outer) loop keeps check on the row number and also printing the pattern and changing line for the next pattern to be printed. This loop also reset the pattern variable for the next pattern.

And the second(inner) loop is generating pattern, by checking the loop condition and if it is true. variable pattern is appended with a "*" in it.



function showStars(rows){

           for(var i = 0; i < rows; i++){ // run row times from 0 to row-1

               var pattern = ""; //resetting the pattern

               for(var j = 0; j < i; j++) //generate the pattern, will run from 0 till i-1

                   pattern = pattern + "*"; //appending the pattern with "*"

           document.writeln(pattern); //printing the pattern when the inner loop is terminated

           document.writeln("<br/>"); //change the line for the next pattern to be printed.

           } //first(outer) loop end

       }

Add a comment
Know the answer?
Add Answer to:
In this Javascript code, why are 2 for loops needed? could you please explain this to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • could you please show me what I am doing wrong in the Javascript code below. <html>...

    could you please show me what I am doing wrong in the Javascript code below. <html>   <head> <meta charset="UTF-8"> <title></title> <style> #mytext { color: white; text-align: center; } </style> <script> var words = "The cat sat on the mat"; document.writeln("id='mytext"+ words '); </script>   </head>   <body>   </body> </html>

  • This question relates to Javascript. Could you please show me why my code is not working?...

    This question relates to Javascript. Could you please show me why my code is not working? I want to create 2 text boxes and have the user enter something into both of them. If the text entered into both boxes is identical the a green message is made visible. If the boxes are different a red message is made visable. Please keep it simple because I am new to javascript. <html> <head>               <title></title>        <script>...

  • Please help me with the following requirements for JavaScript. Write a function to take a temperature value in Celsius a...

    Please help me with the following requirements for JavaScript. Write a function to take a temperature value in Celsius as an argument and return the equivalent temperature in Fahrenheit, basing it on the code from Hour 2. //Original Code <!DOCTYPE html> <html> <head>     <title>Fahrenheit From Celsius</title> </head> <body>     <script>         var cTemp = 100;  // temperature in Celsius         var hTemp = ((cTemp * 9) /5 ) + 32;         document.write("Temperature in Celsius: " + cTemp + " degrees<br/>");         document.write("Temperature in Fahrenheit: "...

  • Javascript program reformation A. combine three loops into one loop and REPLACE FOR LOOP with a...

    Javascript program reformation A. combine three loops into one loop and REPLACE FOR LOOP with a while loop to maintain the same result <!DOCTYPE html> <html><head><title>19.7 For Loop</title> <script language="javascript"> //numbers divisible by 5 for (i=1; i<=50; i++) {    if (i%5 == 0) document.write ("number divisible by five is: " + i + "<br />"); } //for document.write ("========================<br />"); //separator //odd numbers divisible by 7 for (i=1; i<=50; i++) {    if ((i%7==0) && (i%2!=0)) document.write ("odd number...

  • Tip Calculator The code works, but has significant errors when placed in a validator. Can you...

    Tip Calculator The code works, but has significant errors when placed in a validator. Can you provide code that will clear the validation process without error? The validator does not like the "out', indicated it was not defined, then the string of code is bringing up several errors with "stray tag". It has to show no errors, here are the a few coming up, could not post all: table.js out = " Price15 %18%20%"; document.writeln(out); var a, b, c; for(var...

  • Please help me with this code for javascript. <!DOCTYPE html> <html> <head> <title>Strings and Arrays</title> <script>...

    Please help me with this code for javascript. <!DOCTYPE html> <html> <head> <title>Strings and Arrays</title> <script> function wrangleArray() { var string1 = prompt("Enter: This class is going to fast");//Must be entered by the user var string1 = "";//Is this right? I want that string to change later by a series of prompt questions document.getElementById("div1").innerHTML = "<p>" + sentence + "</p>"; var words = sentence.split(" ");//I need to convert my string to an Array is this the way to do it?...

  • Could you please help me write a Javascript code that will show a smiley face picture...

    Could you please help me write a Javascript code that will show a smiley face picture when I click the button. The picture should not display until the button is pressed. Here is my attempt at the code. It does not work. Please keep the code as simple as you can as I am new to coding. Please also use the <div> tag if possible, onclick and use the getElementById. Thank you <html> <body> <img id="exampleImage" src="smileyFace.png" /> <button onclick="pushButton()">Try...

  • Create an HTML form that takes these inputs: Number of rows, and number of columns. This...

    Create an HTML form that takes these inputs: Number of rows, and number of columns. This form will submit to a PHP page. Create that page that will accept input from the HTML form and generates the table from the user's input. I already have the portion that generates the table based on user input by using Javascript in an HTML page, I just need to know how to generate the table using a PHP page instead. Here's the code...

  • LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but,...

    LANGUAGE JAVASCRIPT, PHP Need help with PHP and ajax code. The user needs to login but, I keep getting an error that I coded "Wrong username and password!" ***PLEASE DONT GIVE ME A NEW CODE THAT IS NOWHERE NEAR THE CODE I SUBMITTED***** PLEASE LOOK AT THE CODE AND SEE ANY ISSUES login.html <!DOCTYPE html> <html> <head> <title>login popup</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style type="text/css"> body { background-color: white; } </style> </head> <body> <center> <h1 style="text-align: center;"> Login </h1> <form>             Login ID:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT