Question

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 divisible by seven is: " + i + "<br />");
} //for
document.write ("========================<br />"); //separator

//even numbers divisible by 9
for (i=1; i<=50; i++) {
   if ((i%9==0) && (i%2==0)) document.write ("even number divisible by nine is: " + i + "<br />");
} //for
document.write ("========================"); //separator

</script>
</head>
<body>
</body>
</html>

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

Code:

<!DOCTYPE html>
<html><head><title>19.7 For Loop</title>
<script language="javascript">

//numbers divisible by 5
var div5="";//Variable to store the content obtained for condition of divisability by 5
var div7="";//Variable to store the content obtained for condition of divisability by 7
var div9="";//Variable to store the content obtained for condition of divisability by 9
var i=1;
while (i<=50) {//while looping from 1 to 50
if (i%5 == 0) div5+="number divisible by five is: " + i + "<br />";
if ((i%7==0) && (i%2!=0)) div7+="odd number divisible by seven is: " + i + "<br />";
if ((i%9==0) && (i%2==0)) div9+="even number divisible by nine is: " + i + "<br />";
i++;//incrementing i
}

document.write(div5);//printing the html string to the document
document.write ("========================<br />"); //separator
document.write(div7);//printing the html string to the document
document.write ("========================<br />"); //separator
document.write(div9);//printing the html string to the document
document.write ("========================"); //separator

</script>
</head>
<body>
</body>
</html>

Sample i/o:

Explanation:

A while loop is written to loop from 1 to 50 by using loop variable i. 3 variables were used to store the content that is being displayed on the screen. After the loop ends the whole variable is printed as the html text does not look at spaces and new lines.

Add a comment
Know the answer?
Add Answer to:
Javascript program reformation A. combine three loops into one loop and REPLACE FOR LOOP with a...
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
  • 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: "...

  • 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...

  • <!DOCTYPE HTML> <html lang="en">    <title>JavaScript Array Lab</title>    <script src="script.js"></script>    <body>        <p>To...

    <!DOCTYPE HTML> <html lang="en">    <title>JavaScript Array Lab</title>    <script src="script.js"></script>    <body>        <p>To test your function, call divideArray() from the JavaScript console in the browser.</p>    </body> </html> Don't edit your code on the above code and do not copy from others', plz! // Put your solution here in script.js Write the function divideArray() in script.js that has a single numbers parameter containing an array of integers. The function should divide numbers into two arrays, evenNums for...

  • I am trying to make it so that radio buttons when clicked execute a javascript file....

    I am trying to make it so that radio buttons when clicked execute a javascript file. I have to to have the exercises in external files. Here is what I have so far. I am reusing a script that was used to make radio buttons switch CSS files so I think I have to change the set attribute options. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p> 4.2 Output: The first 20 Fibonacci numbers, which are...

  • Javascript + HTML, no JQuery please. So, Im trying to figure out how to send an...

    Javascript + HTML, no JQuery please. So, Im trying to figure out how to send an alert if the value of my selection is "0". I will be using this for a validation form assignement. So if the user leaves the selection on the default "0" value I can prompt the user to pick a valid option which would be 1+. Not sure how to access this value. Example code below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Form Validation</title>...

  • In an external JavaScript file, you need to create two global variables for the Quarter name...

    In an external JavaScript file, you need to create two global variables for the Quarter name array and the Sales amount array. Create an anonymous function and connect it to the onclick event of the Add to Array button that adds the values from the fields (Quarter and Sales) to the respective arrays. Create an anonymous function and connect it to the onclick event of Display Sales button displays the contents of the arrays, displays the sum of all the...

  • 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...

  • JAVASCRIPT/JQUERY Hello I would like some help understanding jQuery. Here are the directions: Write a program...

    JAVASCRIPT/JQUERY Hello I would like some help understanding jQuery. Here are the directions: Write a program that creates the following table: Course Course Name Day Time IMS115 Windows 10 Monday 6 PM IMS215 Office 2016 Wednesday 5 PM MIS200 Java Thursday 9 AM MTH105 Business Math Saturday 10 AM Using jQuery, select every other odd row and change the font color to blue and make it bold. And here is my code: Chapter4.html <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="Chapter4.css">...

  • This is my code so far: <!DOCTYPE html> <html> <head> <title>JavaScript is fun</title> <meta charset="utf-8" />...

    This is my code so far: <!DOCTYPE html> <html> <head> <title>JavaScript is fun</title> <meta charset="utf-8" /> </head> <body> <script type ="text/javascript"> //declare a variable and store text in it var x = "JavaScript is fun"; //write the variable 5 times document.write(x + x + x + x + x); //store 5 as string in variable x x = "5"; //store 3 as string in variable y var y = "3"; //write the value x + y to the document document.write("<br>");...

  • Task 3: Creating a Simple jQuery Application 1. Launch HTML-Kit. 2. Create a new HTML file...

    Task 3: Creating a Simple jQuery Application 1. Launch HTML-Kit. 2. Create a new HTML file and save it as nnLab8.htm. 3. Add the following HTML to the file: <!DOCTYPE HTML> <html> <head> <title>Hello World - jQuery Style</title> </head> <body> <div id="first"></div> <div id="second"></div> <a href="#" id="link">Click Me!</a><br /> <span id="greeting"></span> </body> </html> 4. Add the following<script> element to the<head> section. NOTE: Use the jQuery version number that matches the file name of the file you downloaded in Step 1....

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