26 Exam3ReviewAns
26 Exam3ReviewAns
1) Write the details of a function that prompts the user for a color and uses what they
typed to set the link color within the existing web page.
function changeLinkColor()
{
var color=prompt(``enter a color please'','''');
document.link=color;
function squares(size)
{
var sqr;
Write a program trace explaining what each line of the program does and give
sample result
define a function called "squares" that takes a single parameter called "size"
this function has a variable called "sqr" defined.
we loop "size" many times using a loop counter variable "i"
Inside the loop, we compute the square of "i" and store the result in "sqr"
Pop-up an alert containing the square of "i" and do that until i=size
a sample result would be the following numbers each in its own alert box:
1
3) Write a function that prompts the user to input a number, and then gives them 5
chances to guess the square of the number.
<script language="JavaScript">
var theNumber = prompt("pick a number","");
var num = parseInt(theNumber);
var theSquare;
var theirNumber;
var guess;
theSquare= num * num;
for (var i=1; i <= 5; i++)
{
theirNumber= prompt("what is " + num + " squared","0");
guess= parseInt(theirNumber);
if (guess == theSquare)
{
alert("Correct");
break;
} else
{
alert("Attempt " + i + " is wrong");
}
}
</script>
4) Write a function that takes 3 parameters, price, tax rate, balance, and prints yes in
an alert box if someone has adequate balance to purchase an item at the specified
price with the given tax rate. Otherwise, print out no in an alert box.
<script language="JavaScript">
function affortIt(price, taxrate, balance)
{
var total = price + price *taxrate;
2
5) You are given the following form...
<form name="exam">
<input type="text" name="first">
<input type="text" name="last">
<input type="button" value="click me" onClick="display()">
</form>
Modify this form in the following fashion
a. Add a function connected to the button to display all the data associated with
the components in the form. This function is to be called whenever the user
clicks the button.
<script language="JavaScript">
function display()
{
alert(document.exam.first.value + " " +
document.exam.last.value);
}
</script>
b. modify the form so that when the user clicks in each of the fields, their
contents are hi-lighted.
<form name="exam">
<input type="text" name="first" onFocus="this.select()"
onChange="display()">
<input type="text" name="last" onFocus="this.select()"
onChange="display()">
<input type="button" value="click me" onClick="display()">
</form>
6) Write a function that generates a web page containing a table with 3 columns and
500 rows. Label each location in the table with row and column location.
function genTable()
{
document.open();
document.write("<html><head><title>the
title</title></head><body><table>");
document.write("</table></body></html>");
document.close();
}
3
7) Write the details of the following function that prompts the user for a color using the
following format:
<body ID="bdy">
<p>
Pick a background color for this page.
</p>
<form name="form1">
<b> Color </b>
<input type="text" name="body"><br>
<input type="button" value="Change color
onclick="changeBgColor()">
<br>
</form>
</body>
and then uses that color to set the background color of the web page.
function changeBgColor()
{
function changeBgColor(){
var bodycolor = document.form1.body.value;
document.getElementById("bdy").style.backgroundColor = bodycolor;
}
return output;
}
Write a program trace that explains what each line of the program does
and give a sample result that is returned.
define a function called "fib" that takes a single parameter called "size"
this function has a array object called "output" defined.
we loop "size" many times using a loop counter variable "i"
Inside the loop, we compute the sum of output[i-1] and output[i-2] and store the result in
output[i]
we return the array output
4
9) Write a function that keeps prompting the user for input until the letter "p" is typed by
the user.
function iqTest()
{
var letterP = prompt("Please enter the letter p"," ");
while (letterP!=="p")
{
letterP = prompt("No dummy! Enter the letter p","");
}
alert("Thank you!")
}
10) Write a function that takes 4 variables, a, b, c, d, and prints true in an alert box if
input a > input b and input c > input d. Else, print false in an alert box
<script language="JavaScript">
function question9(a,b,c,d)
{
if (a>b&&c>d) {
alert("true")
}
else {
alert("false")
}
}
</script>
11) Write a function that generates a complete web page containing a list with 50 items.
The items should be item1 through item50
<script>
function genList()
{
document.open();
document.write("<html><head></head><body>");
document.write("<ul>");
for (var i=1; i <= 50; i++)
{
document.write("<li> item" + i + "</li>");
}
document.write("</ul></body></html>");
document.close();
}
</script>
5
12) Write a function tip(bill) that will calculate and return the amount that should be left
as a tip for a restaurant bill when passed the parameter bill. The tip should be 20%
of the total before a tip is added.
<html>
<head>
<title>Question 9</title>
<script language="JavaScript">
function tip(bill)
{
var bill, t;
t = .2*bill;
return t;
}
</script>
</head>
<body>
<script>
var a = prompt("how much is the bill?","");
var af = parseFloat(a);
t = tip(af);
alert("Please leave a tip of $" +t)
</script>
</body>
</html>
13) Write a function that prompts the user for a color and uses what they typed to set the
background color of the web page.
<html>
<head><title>Changing Background Color</title>
<script language="JavaScript">
function changeBgColor(){
var bodycolor = document.form1.body.value;
document.getElementById("bdy").style.backgroundColor = bodycolor;
}
</script>
</head>
<body ID="bdy">
<p>
Pick a background color for this page.
</p>
<form name="form1">
<b> Color </b>
<input type="text" name="body"><br>
<input type="button" value="Change color" onclick="changeBgColor()">
<br>
</form>
</body>
</html>
6
14) Write a function that takes 4 variables, a, b, c, d, and prints true in an alert box if
input a ≥ input b and input c ≤ input d. Else, print false in an alert box.
<html>
<head>
<title>Question I-3</title>
<script language="JavaScript">
function question9()
{
var a = prompt("Give me an \"a\"","");
var af = parseFloat(a);
var b = prompt("Give me an \"b\"","");
var bf = parseFloat(b);
var c = prompt("Give me an \"c\"","");
var cf = parseFloat(c);
var d = prompt("Give me an \"d\"","");
var df = parseFloat(d);
if (af>=bf&&cf<=df) {
alert("true")
}
else {
alert("false")
}
}
</script>
</head>
<body>
<form>
<input type="button" value="determine conditional"
onclick="question9()">
</form>
</body>
</html>
<script type="text/javascript">
function fact(n)
{
var n, retval=1;
for(i=n;i>0;i--){
retval=retval*i;
}
return retval;
}
</script>
7
a) Write a program trace that explains what each line of the program does and give
a sample of the result that is returned.
Input “n” via some mechanism, e.g., a prompt box
16) Write a function that loops through heading tags (h1-h6) and generates the following
web page (Note: “this is heading n” should be formatted using an <hn> tag as in
<h1>This is heading 1</h1>).
<html>
<body>
<script type="text/javascript">
for (i = 1; i <= 6; i++)
{
document.write("<h" + i + ">This is heading " + i);
document.write("</h" + i + ">");
}
</script>
</body>
</html>
8
17) Assume you have created an array of pets:
<script type="text/javascript">
var mypets = new Array();
mypets[0] = "dog";
mypets[1] = "cat";
mypets[2] = "goldfish";
</script>
a) Add a for loop to print out the array (add code below): [5 pts.]
<script type="text/javascript">
var mypets = new Array();
mypets[0] = "dog";
mypets[1] = "cat";
mypets[2] = "goldfish";
for (i=0;i<mypets.length;i++)
{
document.write(mypets[i] + "<br />");
}
</script>
b) Modify the JavaScript to create a second array, mynewpets, containing turtle and
gerbil. Modify mypets to append mynewpets, so that the for loop will now print
out the modified 5-element array mypets using the same for loop as above. Add
code below: [10 pts.]
<script type="text/javascript">
var mypets = new Array();
mypets[0] = "dog";
mypets[1] = "cat";
mypets[2] = "goldfish";
var mynewpets = new Array();
<html>
<body>
<script type="text/javascript">
var mypets = new Array();
mypets[0] = "dog";
mypets[1] = "cat";
mypets[2] = "goldfish";
mypets = mypets.concat(mynewpets)
for (i=0;i<mypets.length;i++)
{
document.write(mypets[i] + "<br />");
}
</script>
</body>
</html>
9
1) Write a function share(bill,sizeGroup) that takes two parameters, bill and sizeGroup,
calculates and returns the amount (share) that each person should pay when the bill is
divided equally among sizeGroup diners using an alert box. A tip of 20% of the total should
be computed and added to the bill before computing each person’s share. Write ONLY the
function (see next question) [5 pts.]
function share(bill,sizeGroup) {
var share= (bill*1.2)/sizeGroup;
share=share.toFixed(2);
alert("Each person owes $" + share)
}
2) Suppose one calls the function share(bill,sizeGroup) in 1) above using the code below.
a) Add the necessary statements to insure that the inputs, bill and sizeGroup, are converted
to the correct type and precision, e.g., bill is floating point with two decimal points (12.65
and not 12.6534) and sizeGroup is an integer before passing to share(bill,sizeGroup). [4
pts]
<script>
sizeGroup=parseInt(sizeGroup);
share(bill,sizeGroup);
</script>
3) Modify the script below, so that “I love CMPSCI120” is printed. Hint: use one of the string
object methods [5 pts.]
<html>
<head>
</head>
<body>
<script type="text/javascript">
var str="I hate CMPSCI120";
document.write(str.replace("hate","love") );
</script>
</body>
</html>
10
4) Write a function question4() that asks for 4 inputs (a, b, c, d) using prompt boxes, converts
them to floating point, and prints true in an alert box if (input a x input b) < (input c x input
d). Else, print false in an alert box. [5 pts.]
<html>
<head>
<title>Question JI-4</title>
<script language="JavaScript">
function question9()
{
var a = prompt("Give me an \"a\"","");
var af = parseFloat(a);
var b = prompt("Give me an \"b\"","");
var bf = parseFloat(b);
var c = prompt("Give me an \"c\"","");
var cf = parseFloat(c);
var d = prompt("Give me an \"d\"","");
var df = parseFloat(d);
if ((af*bf)<(cf*df)) {
alert("true")
}
else {
alert("false")
}
}
</script>
</head>
<body><form>
<input type="button" value="determine conditional"
onclick="question9()">
</form>
</body>
11
5) You are given the following function …
<script type="text/javascript">
function primeNum()
{
var p =prompt("enter a postive integer less than 100", "
");
p=parseInt(p);
while (p>=100) {var p =prompt("I said less than 100!!", "
");}
var isPrime=1;
for (var i=2; i<p; i++)
{
var modI=p%i;
if (modI == 0)
{
isPrime=0;break;
}
}
if (isPrime)
{alert(p + " is prime!")}
else {alert(p + " is not prime!")}
}
</script>
Explain via a step by step program trace what the function primeNum() does.
12
6) Write a function TablePrint(r,c) that takes two parameters, r and c, and prints a table with r
rows and c columns where each table entry is “[r,c]”. Here is an example output for r= 5,
c=6:
Write ONLY the function, not the full script with inputs.
function genTable(r,c)
{
document.write("<table>");
13
Assume you have created an array of UMass sports:
var umassSports = new Array();
umassSports[0] = "Football";
umassSports[1] = "Hockey";
umassSports[2] = "Basketball";
umassSports[3] = "Swimming";
umassSports[4] = "Track";
umassSports[5] = "Skiing";
a) Add code below to print out the array as shown: [4 pts.]
<script language="JavaScript">
var umassSports = new Array();
umassSports[0] = "Football";
umassSports[1] = "Hockey";
umassSports[2] = "Basketball";
umassSports[3] = "Swimming";
umassSports[4] = "Track";
umassSports[5] = "Skiing";
document.write("<b>UMass Sports Include: "+ umassSports + "<br>");
</script>
b) Modify the JavaScript to create a new array, aftercutsSports, that removes Football and
Skiing and prints out the results as shown below. Hint: use the appropriate method for the
Array object. [6 pts.]
<script language="JavaScript">
var umassSports = new Array();
umassSports[0] = "Football";
umassSports[1] = "Hockey";
umassSports[2] = "Basketball";
umassSports[3] = "Swimming";
umassSports[4] = "Track";
umassSports[5] = "Skiing";
document.write("<b>UMass Sports Include: "+ umassSports + "<br>");
var aftercutsSports=umassSports.slice(1, 5);
document.write("<b>UMass Sports After the budget cuts
Include: "+ aftercutsSports + "<br>");
</script>
14
4) Complete the following function, so that the function greetKitty()inputs from the text
box (you need to reference the text box to extract the value) and outputs an alert box that says
Hello name as shown below. [10 pts.]
<html>
<head>
<script type="text/javascript">
function greetKitty()
{
var name = document.getElementById("myText").value;
alert("Hello " + name);
}
</script>
</head>
<body>
<p> what is your Cat's name?</p>
<form>
<input size="25" type="text" id="myText" value="Garfield">
<input type="button" value="Input" onclick="greetKitty()">
</form>
</body>
</html>
15
W3Schools JAVASCRIPT & (limited) PHP Quizzes
4) What is the correct syntax for referring to an external script called "xxx.js"?
a) <script name="xxx.js">
b) <script href="xxx.js">
c) <script src="xxx.js">
16
9) How do you write a conditional statement for executing some statements only if "i" is
equal to 5?
a) if i==5 then
b) if (i==5)
c) if i=5 then
d) if i=5
10) How do you write a conditional statement for executing some statements only if "i" is
NOT equal to 5?
a) if (i <> 5)
b) if <>5
c) if =! 5 then
d) if (i != 5)
15) What is the correct JavaScript syntax to insert a comment that has more
a) //This comment has
more than one line//
b) <!--This comment has
more than one line-->
c) /*This comment has
more than one line*/
17
16) What is the correct way to write a JavaScript array?
a) var txt = new Array(1:"tim",2:"kim",3:"jim")
b) var txt = new Array("tim","kim","jim")
c) var txt = new Array="tim","kim","jim"
d) var txt = new Array:1=("tim")2=("kim")3=("jim")
17) How do you round the number 7.25, to the nearest whole number?
a) round(7.25)
b) Math.round(7.25)
c) rnd(7.25)
d) Math.rnd(7.25)
19) What is the correct JavaScript syntax for opening a new window called "window2" ?
a) new.window("http://www.w3schools.com","window2")
b) window.open("http://www.w3schools.com","window2")
c) open.new("http://www.w3schools.com","window2")
d) new("http://www.w3schools.com","window2")
18
23) PHP server scripts are surrounded by delimiters, which?
a) <script>...</script>
b) <?php…?>
c) <&>...</&>
d) <?php>...</?>
29) How do you write a conditional statement for executing some statements only if "i" is
NOT equal to 5? [4 pts.]
a) if (i <> 5)
b) if <>5
c) if =! 5 then
d) if (i != 5)
19
31) How do you put a message in the browser's status bar? [4 pts.]
a) window.status("put your message here")
b) statusbar = "put your message here"
c) window.status = "put your message here"
d) status("put your message here")
a)
b)
c)
20
34) How do you find the largest number of 2 and 4? [4 pts.]
a) Math.max(2,4)
b) ceil(2,4)
c) top(2,4)
d) Math.ceil(2,4)
21
Multiple Choice Questions (4 pts. each). Circle the letter of the best answer.
22
1. What should do you to protect your computer from virus attacks? List three
different things you can do.
Use a antivirus scanner
Update the virus definitions
Contact your IT office
Remove your computer from the internet
Keep bootable media out of your drive on startup
Use a firewall
Medium-Long answer questions. For Exam 2, these will include CSS and/or html or
layout questions.
1. Explain how public key encryption works. Explain both the steps and the
details.
23
Each person has two keys, a public key K+ and a private key K-. To send a
message, Alice can encrypt a message with Bob’s public key, which can
only be decrypted using Bob’s private key. [This is not required] Similarly,
Alice can encrypt a message with her private key, which can be encrypted
by anyone using her public key … thus identifying Alice as the sender
(digital signature).
24
1. Which of the following is correct?
a. jQuery is a JSON Library
b. jQuery is a JavaScript Library
6. The jQuery html() method works for both HTML and XML documents
– False or True T
7. What is the correct jQuery code to set the background color of all p
elements to red?
a. $("p").style("background-color","red");
b. $("p").layout("background-color","red");
c. $("p").manipulate("background-color","red");
d. $("p").css("background-color","red");
25
10. Which jQuery method is used to set one or more style properties for
selected elements?
a. style()
b. css()
c. html()
11. What is the correct jQuery code for making all div elements 100
pixels high?
a. $("div").height(100)
b. $("div").yPos(100)
c. $("div").height="100"
14. Which jQuery function is used to prevent code from running, before
the document is finished loading?
a. document.ready()
b. body.onload()
c. document.load()
26