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...
1. What is the correct way to write a JavaScript array? var colors = (1:"red", 2:"green", 3:"blue") var colors = ["red", "green", "blue"] var colors = 1 = ("red"), 2 = ("green"), 3 = ("blue") var colors = "red", "green", "blue" 2. Which event occurs when the user clicks on an HTML element? onclick onmouseover onchange onmouseclick 3. How do you declare a JavaScript variable? v carName; var carName; variable carName; 4. Javascript is A client-side scripting language that is...
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...
write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...
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...
For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following: Display a pyramid of asterisks onscreen (i.e., a nested for loop) Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop) Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number...
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...
PROJECT TASKS 1. Read the problem definition below and write a C++ program that implements your solution. Readability of the program will be graded based on variable naming, indentation, commenting (not too little and not too much), spacing, consistency, and styling in general including choice of functions. [NOTE: the code at the end of this document does not completely meet requirements as it uses many single letter variables and has no internal documentation. You need to improve it.] 2. Compile...