Hi all, I'm new to "advance" javascript and I need some help I dont mind if it's not written for me but a point in the right direction.
I've been using your book "Beginning JavaScript, 2nd Edition", and working through the ch 4 demo about object programming.
I'm building an app, that takes the contents of an Array (which will eventually be dynamic) and printing the array out via a specific formation.
I've written pretty much the shell of program, although I'll be re-writing the output to be done via DOM. ANYWAYS...
I've hit a brick wall due to two problems:
1) The first is how can I sort the array, as no matter where I put cinema.sort() in my app it crashes it. I want it sorted by localeGroup.
2) How can I dynamically put in a new <tr><td></td></tr> tag at the top and bottom of each new iteration of a localeGroup, BUT ONLY THE FIRST INSTANCE OF new localeGroup.
I.E. - Currently it outputs the array as it comes in nothing more, but I need to put in a <tr><td></td></tr> structure at the start of a new set of data. When it loops through the array when it sees a new localeGroup it'll put in a <tr><td></td></tr> and another <tr><td></td></tr> at the last element in that localeGroup.
current output:
[ malaga ][ £99 ][ Europe ]
[ orlando ][ £99 ][ USA ]
[ london ][ £99 ][ Europe ]
what i need:
[ NEW <tr><td></td></tr> ] <-- Element new localeGroup
[ malaga ][ £99 ][ Europe ]
[ london ][ £99 ][ Europe ]
[ NEW <tr><td></td></tr> ] <-- Element last localeGroup
[ NEW <tr><td></td></tr> ] <-- Element new localeGroup
[ orlando ][ £99 ][ USA ]
[ NEW <tr><td></td></tr> ] <-- Element last localeGroup
Cheers for any help you give me, hopefully I've explained myself correctly. I'm having trouble taking my Javascript skills to the next level. Hopefully someone will make my brain click :D
heres WORKING source for so far:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<h2>Prices Array Printing/Sorting</h2>
<script type="text/javascript">
function dynamicPricing (localeName,localePrice, localeGroup) {
this.localePrice = localePrice;
this.localeName = localeName;
this.localeGroup = localeGroup;
}
dynamicPricing.prototype.getLocalePrice = function() {
return this.localePrice;
}
dynamicPricing.prototype.setLocalePrice = function(localePrice) {
this.localePrice = localePrice;
}
dynamicPricing.prototype.getLocaleGroup = function() {
return this.localeGroup;
}
dynamicPricing.prototype.setLocaleGroup = function(localeGroup) {
this.film = localeGroup;
}
dynamicPricing.prototype.getLocaleName = function() {
return this.localeName;
}
dynamicPricing.prototype.setLocaleName = function(localeName) {
this.localeName = localeName;
}
function cinema()
{
this.bookings = new Array();
}
cinema.prototype.addLocale = function(localeName, localePrice, localeGroup)
{
this.bookings[localeName] = new dynamicPricing(localeName, localePrice, localeGroup);
}
cinema.prototype.getBookingsTable = function()
{
var booking;
var bookingsTableHTML = "<table border=1>";
for (booking in this.bookings)
{
bookingsTableHTML += "<tr><td>";
bookingsTableHTML += this.bookings[booking].getLocaleName();
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getLocalePrice();
bookingsTableHTML += "</td>";
bookingsTableHTML += "<td>";
bookingsTableHTML += this.bookings[booking].getLocaleGroup();
bookingsTableHTML += "</td>";
bookingsTableHTML += "</tr>"
}
bookingsTableHTML += "</table>";
return bookingsTableHTML;
}
var pricingTable = new cinema();
pricingTable.addLocale("malaga", "£99", "Europe");
pricingTable.addLocale("orlando", "£99", "USA");
pricingTable.addLocale("london", "£99", "Europe");
document.write(pricingTable.getBookingsTable());
</script>
</body>
</html>