0% found this document useful (0 votes)
26 views

JS Practice (Chapter 5)

The document provides examples of using JavaScript to work with strings, arrays, objects, and dates. It includes code samples for counting substring occurrences in a string, checking the case of a character, sorting an array, creating a CustomerBooking object type with methods, and creating a Cinema object to store bookings. It concludes with exercises asking the reader to work with dates, sort an array of names from user input, and fix a number formatting issue.

Uploaded by

Nyein Htut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

JS Practice (Chapter 5)

The document provides examples of using JavaScript to work with strings, arrays, objects, and dates. It includes code samples for counting substring occurrences in a string, checking the case of a character, sorting an array, creating a CustomerBooking object type with methods, and creating a Cinema object to store bookings. It concludes with exercises asking the reader to work with dates, sort an array of names from user input, and fix a number formatting issue.

Uploaded by

Nyein Htut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

JavaScript

Examples and Exercises


Chapter_5
String
Object’s Examples

Counting Occurrences of Substrings: ch5_examp1.htm


<html xmlns=“http://www.w3.org/1999/xhtml”> while (foundAtPosition != -1)
<head> {
<title>Chapter 5: Example 1</title> foundAtPosition =
</head> myString.indexOf(“Wrox”,foundAtPosition);
<body> if (foundAtPosition != -1)
<script type=“text/javascript”> {
var myString = “Welcome to Wrox books.”;
wroxCount++;
foundAtPosition++;
myString = myString + “The Wrox website is www.wrox.com.”;
}
myString = myString + “Visit the Wrox website today. Thanks for buying }
Wrox”; document.write(“There are “ + wroxCount + “
occurrences of the word Wrox”);
var foundAtPosition = 0; </script>
var wroxCount = 0; </body>
</html>
Faculty of Information Science 2
String
Object’s Examples

Checking a Character’s Case :ch5_examp2.html.


<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 5: Example 2</title>
<script type=“text/javascript”>
function checkCharType(charToCheck)
{
var returnValue = “O”;
var charCode = charToCheck.charCodeAt(0);
if (charCode >= “A”.charCodeAt(0) && charCode <= “Z”.charCodeAt(0))
{
returnValue = “U”;
} Faculty of Information Science
String
Object’s Examples

else if (charCode >= “a”.charCodeAt(0) && charCode <= “z”.charCodeAt(0))


{
returnValue = “L”;
}
else if (charCode >= “0”.charCodeAt(0) && charCode <= “9”.charCodeAt(0))
{
returnValue = “N”;
}

return returnValue;
}
</script>
</head>

Faculty of Information Science


<body>
String
<script type=“text/javascript”> Object’s Examples
var myString = prompt(“Enter some text”,”Hello World!”);
switch (checkCharType(myString))
{
case “U”:
document.write(“First character was upper case”);
break;
case “L”:
document.write(“First character was lower case”);
break;
case “N”:
document.write(“First character was a number”);
break;
default:
document.write(“First character was not a character or a number”);
}
</script>
</body>
</html>
Faculty of Information Science
Array
Object’s Examples Sorting an Array :ch5_examp3.html

<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

else if (ord == -1)


{
myShopping.sort();
myShopping.reverse();
document.write(myShopping.join(“<br />”));
}
else
{
document.write(“That is not a valid input”);
}
</script>
</body>
</html>

Faculty of Information Science


Reference
Examples
ch5_examp8.html
Type

<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;
}

Faculty of Information Science 10


// Cinema type
Reference
Type Examples

function Cinema()
{
this.bookings = new Array();
}

Cinema.prototype.addBooking = function(bookingId, customerName, film, showDate)


{
this.bookings[bookingId] = new CustomerBooking(bookingId,
customerName, film, showDate);
}

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>

Faculty of Information Science 13


Exercises Questions
Exercise Questions

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.

Faculty of Information Science 14


Exercises Questions

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

• However, your fix() function instead returns it like this:

• 2.1

• Change the fix() function so that the additional zeros are added where necessary.

Faculty of Information Science 15


ch05_q1.htm
Exercises Answers

<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>

You might also like