LAB 4: JavaScript Loops
Part I)
1) Make an array of colors, instead of responses, call it colors.
Color names go inside quotes, i.e. "red".
2) Copy the line of the var randomNum, but instead of looking at
the length of responses, look at the length of your colors
array.
3) Declare a variable myColor below the randomNum variable
declaration.
4) Save a randomly chosen color into the variable myColor.
colors[randomNum] picks a random color from the
array. Save that into the variable myColor using =
5) Write the name of the randomly chosen color to the document. See
writing statement in the participation activity. Your variable
myColor, with the chosen color saved inside it, must be
concatenated to the string written to the document. Instead of
responses[randomNum].
Use something like "The chosen color is ... " + myColor + "
..."
6) Apply the chosen color to make it the background color of the
document.
Use document.bgColor = myColor;
Careful, please recall that JavaScript is case sensitive! Don't
forget semicolons at the end of each statment.
Check that it works! Every time you refresh the page, a new color
paints the page and it is written on the document.
Part II)
Loops:
Instead of writing to the console, write to the document. There is
an example loop in the initial file. After each loop place the
following statement.
document.writeln("<br>");
Reference: Participation Activities 8.4.6, 8.4.7
1) Write a for loop to print the following sequence of
numbers.
0 1 2 3 4 5 6 7 8 9
2) Write a for loop to print the following sequence of
numbers.
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
Reference: Participation Activity 8.4.2
Note typo on 3, there should be no ; after while (c <= 20)
3) Write a while loop to print the following sequence of
numbers.
Same as before, write into document. Use document.write(c+"
");
5 10 15 20 25 30 35 40 45 50
4) Write a while loop to print the following sequence of
numbers.
8 10 12 14 16 18
JavsScript along with HTML:
(Part I)
<html>
<head>
<script
type="text/javascript">
//Function that
gets called on body load event
function
changeColor()
{
//Color array
var colors = ["Aqua", "Crimson", "DarkViolet",
"Khaki", "Orchid"];
//Creating a random variable using colors array
length
var randomNum = Math.floor(Math.random() *
colors.length);
//Selecting random color
var myColor = colors[randomNum];
//Writing selected color
document.writeln("The chosen color is ... " +
myColor + " ...");
//Changing background color
document.bgColor = myColor;
}
</script>
</head>
<body onload="changeColor()">
</body>
</html>
Sample Run:
____________________________________________________________________________________________
(Part II)
<html>
<head>
<script
type="text/javascript">
//Function that
gets called on body load event
function
loops()
{
//Looping variable
var i;
//(1) for loop to print the following sequence
of numbers. 0 1 2 3 4 5 6 7 8 9
//Iterating from 0 to 9
for(i=0; i<=9; i++)
{
//Writing to document
document.writeln(i + "
");
}
document.writeln("<br>");
//(2) for loop to print the following sequence
of numbers. -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
10
//Iterating from -10 to 10
for(i=-10; i<=10; i++)
{
//Writing to document
document.writeln(i + "
");
}
document.writeln("<br>");
//(3) Using while loop 5 10 15 20 25 30 35 40 45
50
i = 5;
//Loop till i is less than 50
while(i<=50)
{
//Writing to document
document.writeln(i + "
");
//Incrementing i by 5
i = i+5;
}
document.writeln("<br>");
//(4) Using while loop 8 10 12 14 16 18
i = 8;
//Loop till i is less than 50
while(i<=18)
{
//Writing to document
document.writeln(i + "
");
//Incrementing i by 5
i = i+2;
}
}
</script>
</head>
<body onload="loops()">
</body>
</html>
Sample Run:
LAB 4: JavaScript Loops Part I) 1) Make an array of colors, instead of responses, call...
Part 1: Read 2 numbers from the user. One small number and one big number for the range. Use this in a for loop with range (for x in range(small, big)) Add all the numbers in this range and store in a variable called total. Print total. Part 2: Make a list of colors called COLORS. Have at least 10 colors. Read one color from the user in a variable called color. Now use a for loop, and see if...
Part I Short Answer (50 points) 1. Write a pseudocode declaration for a String array initialized with the following strings: “Einstein”, “Newton”, “Copernicus”, and “Kepler”. 2. Assume names in an Integer array with 20 elements. Write a pseudocode algorithm a For Loop that displays each element of the array. 3. Assume the arrays numberArray1 and numberArray2 each have 100 elements. Write a pseudocode algorithm that copies the values in numberArray1 to numberArray2 . 4. Write a pseudocode algorithm for a...
Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in from math import * to make the functions in the math module available. Create a variable and assign into it a constant positive integer number of your choice. The number should be at most 10. 1 Suppose we call this variable x for this writeup document Try something like x = 4 2. Create another...
Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...
Write a program in javascript, that take numbers from 1….20 in an array. Separate Even and Odd numbers by using conditional statement. Figure # 01 is the output of this file on web browser. Example: Odd Number = 1 Even Number = 2 Odd Number = 3 Even Number = 4 Odd Number = 5 Even Number = 6 Odd Number = 7 Even Number = 8 Odd Number = 9 Even Number = 10 Odd Number = 11 Even...
Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...
please do it in C# language Q. 1 part 1 Create an array named weekly Temp that will hold values of weekly temperatures in Fahrenheit from Monday through Sunday. Calculate the average temperature for that week and display its average. part 2 Write while loop to display the numbers from 10 through 1 and the squares. The output should display as following: squared 100 81 04 part 3 Define a method that calculates calculateareaOfCircle by passing a value of radius...
c++ fibonacci code using loops Here are 8 Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21 Note that the first Fibonacci number is 1, F(1) = 1 The second Fibonacci number is 1, i.e. F(2) = 1 Other Fibonacci numbers in the sequence is the sum of two previous Fibonacci numbers. For example F(3) = F(2) + F(1). In general F(n) = F(n-1) + F(n-2) Write a program to do the following tasks. User entries are shown in...
Starting out with Python 4th edition I need help with while loops I can't even get one started correctly. Write a program a program that predicts the approximate size of a population of organisms. Problem 4.13 – population Use a while loop; there are no text boxes, just regular input statements Enter number of organisms: 2 Enter average daily increase: 30 Enter number of days to multiply: 10 DAY APPROXIMATE POPULATION ------------------------------------------------- 1 2 2 2.600 3 3.380 4 4.394...
JAVA I. Using the Event Class created previously, create an array of objects; II. Demonstrate passing array reference to a method; III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...