Вы находитесь на странице: 1из 2

// Christopher Mann 11-22-15 CIS 255 JavaScript Final

// This statement gets a value from the user that is used to decide which option
is chosen in the switch statement.
var getUserChoice = prompt("Please choose an option from the menu by typing 1, 2
, or 3.", "");
// This switch statement is based on the menu written in the HTML code to choose
which programming feature to display.
switch (getUserChoice) {
// This case uses a loop to write a message to the document ten times.
case "1" :
document.write("You chose option #1 Using a Loop.<br><br>");
for (var count = 1; count < 11; count++) {
document.write("This message will be displayed ten times
.<br>");
}; break;
// This case prompts the user for a number and then does a calculation d
epending on whether the number is above or below the number fifty.
case "2" :
document.write("You chose option #2 Use Math.<br><br>");
var num = prompt("Please enter a number", "");
if (num > 50) {
num *= 2;
num = Math.sqrt(num);
} else {
num = +num + 5;
num *= num;
}
alert("Your number is " + num); break;
// This case prompts the user for ten grades and then averages them and
displays that average.
case "3" :
document.write("You chose option #3 Average Grade. Enter ten gra
des to be averaged.<br><br>");
var gradeArray = new Array(10),
total = 0;
for (var count1 = 1; count1 < 11; count1++) {
gradeArray[count1 - 1] = prompt("Please enter grade #" +
count1, "");
total += +gradeArray[count1 - 1];
};
total /= 10;
alert("The average of the ten grades is " + total); break;
// This message is displayed if the user did not choose a valid menu opt
ion.
default :
alert("That is not a menu choice. Please reload the page.");
}
// These statements display a paragraph on the web page that describes what an o
bject in JavaScript programming is and gives examples of objects.
var objectMessage = document.getElementById("objectMessage");
objectMessage.innerHTML = "<p>Objects in JavaScript are useful for passing many

values between places and are also used to organize data by describing a general
object structure and then making particular instances of that object that use t
he same properties.</p><p>JavaScript has many useful objects, such as the Docume
nt object, which is the base of each individual HTML page in a browser, the Wind
ow object, which each window in a browser is made of, and the Date object, which
lets you get the current date and time or set the date and time for an object.<
/p>";
// The following functions take care of setting, reading, and display the cookie
for the page. The last function tests for the existence of a cookie and calls
the appropriate function to set or read it.
function setCookie() {
var cookieValue = "name=Christopher Mann",
expDate = ";expires=Mon, 14 Dec 2015 12:00:00 UTC",
cookieValue = cookieValue + expDate,
cookieValue = encodeURIComponent(cookieValue);
document.cookie = cookieValue;
}
function readCookie() {
var myCookie = document.cookie,
myCookie = decodeURIComponent(myCookie),
myCookie = myCookie.split(";"),
cookieMessage = document.getElementById("cookieMessage");
alert(myCookie[0] + " " + myCookie[1]);
}
if (document.cookie) {
readCookie();
} else {
setCookie();
readCookie();
}

Вам также может понравиться