JS Practice (Chapter 5)
JS Practice (Chapter 5)
return returnValue;
}
</script>
</head>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 5: Example 3</title>
</head>
<body>
<script type=“text/javascript”>
var myShopping = new Array(“Eggs”, “Milk”, “Potatoes”, “Cereal”, “Banana”);
var ord = prompt(“Enter 1 for alphabetical order, and -1 for reverse order”, 1);
if (ord == 1)
{
myShopping.sort();
document.write(myShopping.join(“<br />”));
Faculty of Information Science
}
Array
Object’s Examples
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Chapter 5: Example 8</title>
</head>
<body>
<h2>Summary of bookings</h2>
<script type="text/javascript">
// CustomerBooking type
function CustomerBooking(bookingId, customerName, film, showDate)
{
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
Faculty of Information Science
}
Reference
CustomerBooking.prototype.getCustomerName = function() Type Examples
{
return this.customerName;
}
CustomerBooking.prototype.setCustomerName = function(customerName)
{
this.customerName = customerName;
}
CustomerBooking.prototype.getShowDate = function()
{
return this.showDate;
}
CustomerBooking.prototype.setShowDate = function(showDate)
{
this.showDate = showDate;
} Faculty of Information Science
CustomerBooking.prototype.getFilm = function() Reference
Type Examples
{
return this.film;
}
CustomerBooking.prototype.setFilm = function(film)
{
this.film = film;
}
CustomerBooking.prototype.getBookingId = function()
{
return this.bookingId;
}
CustomerBooking.prototype.setBookingId = function(bookingId)
{
this.bookingId = bookingId;
}
function Cinema()
{
this.bookings = new Array();
}
Cinema.prototype.getBookingsTable = function()
{
var booking;
var bookingsTableHTML = "<table border=1>";
Faculty of Information Science 11
Reference
for (booking in this.bookings) Type Examples
{
bookingsTableHTML += "<tr><td>";
bookingsTableHTML += this.bookings[booking].getBookingId();
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getCustomerName();
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getFilm();
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getShowDate();
bookingsTableHTML += "</td>";
bookingsTableHTML += "</tr>";
}
Faculty of Information Science 12
Reference
Type Examples
bookingsTableHTML += "</table>";
return bookingsTableHTML;
}
var londonOdeon = new Cinema();
londonOdeon.addBooking(342, "Arnold Palmer","Toy Story", "15 July 2009 20:15");
londonOdeon.addBooking(335, "Louise Anderson", "The Shawshank Redemption", "27 July 2009 11:25");
londonOdeon.addBooking(566, "Catherine Hughes", "Never Say Never", "27 July 2009 17:55");
londonOdeon.addBooking(324, "Beci Smith","Shrek", "29 July 2009 20:15");
document.write(londonOdeon.getBookingsTable());
</script>
</body></html>
1. Using the Date type, calculate the date 12 months from now and write this into a
web page.
2. Obtain a list of names from the user, storing each name entered in an array. Keep
getting another name until the user enters nothing. Sort the names in ascending order
and then write them out to the page, with each name on its own line.
3. In this chapter, you learned about how you can use the pow() method inventively to fix a
number to a certain number of decimal places. However, there is a flaw in the function you
created. A proper fix() function should return 2.1 fixed to three decimal places like this:
• 2.100
• 2.1
• Change the fix() function so that the additional zeros are added where necessary.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<script type="text/javascript">
var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",”Sep”,”Oct”,”Nov”,”Dec”);
var nowDate = new Date();
nowDate.setMonth(nowDate.getMonth() + 12);
document.write("Date 12 months ahead is " + nowDate.getDate());
document.write(" " + months[nowDate.getMonth()]);
document.write(" " + nowDate.getFullYear());
</script>
</body>
</html>
Faculty of Information Science 16
Exercises Answers ch05_q2.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head> </head>
<body>
<script type="text/javascript">
var inputName = "";
var namesArray = new Array();
while ( (inputName = prompt("Enter a name","")) != "" )
{
namesArray[namesArray.length] = inputName;
}
namesArray.sort();
var namesList = namesArray.join("<br/>")
document.write(namesList);
</script></body>
</html> Faculty of Information Science 17
Exercises
ch05_q3.htm
Answers
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function fix(fixNumber, decimalPlaces)
{
var div = Math.pow(10,decimalPlaces);
fixNumber = new String(Math.round(fixNumber * div) / div);
if (fixNumber.lastIndexOf(".")==-1)
{
fixNumber = fixNumber + ".";
}
var zerosRequired = decimalPlaces - (fixNumber.length - fixNumber.lastIndexOf(".") - 1);
Faculty of Information Science 18
for (; zerosRequired > 0; zerosRequired--) Exercises Answers
{
fixNumber = fixNumber + "0";
}
return fixNumber;
}
</script>
</head>
<body>
<script type="text/javascript">
var number1 = prompt("Enter the number with decimal places you want to fix","");
var number2 = prompt("How many decimal places do you want?","");
document.write(number1 + " fixed to " + number2 + " decimal places is: ");
document.write(fix(number1,number2));
</script>
</body>
Faculty of Information Science 19
</html>