d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Cant Figure Out Why This Isnt Working > Javascript
Add Reply New Topic New Poll
Member
Posts: 16,399
Joined: Mar 28 2009
Gold: 22.69
Jul 29 2015 08:17pm
Code
<!DOCTYPE html>
<body>

<h2>Summary of bookings</h2>

<script type="text/javascript">
// customer booking type
function CustomerBooking(bookingId, customerName, film, showDate)
{
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
CustomerBooking.prototype.getCustomerName = function()
{
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;
};
CustomerBooking.prototype.getFilm = function()
{
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;
};

// cinema type

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

for (booking in this.bookings)
{
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>";
}

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>



Its supposed to look like this:



When I run the code all that appears is "Summary of bookings" at the top. The table doesnt even appear.


On JShint, the errors I get and dont know how to fix are:

1. The array literal notation [] is preferable.

2. The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.

This post was edited by Shakti on Jul 29 2015 08:22pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 29 2015 08:29pm
what steps have you taken to debug it? use console.log or alert statements or something so you can see the flow.

offhand, this looks suspicious to me.

Code
this.bookings = new Array();


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


Code
for (booking in this.bookings)


in the first and third, you're treating it like an array, but in the second you're treating it like a hashtable?

This post was edited by carteblanche on Jul 29 2015 08:30pm
Member
Posts: 16,399
Joined: Mar 28 2009
Gold: 22.69
Jul 29 2015 08:53pm
Quote (carteblanche @ Jul 29 2015 09:29pm)
what steps have you taken to debug it? use console.log or alert statements or something so you can see the flow.

offhand, this looks suspicious to me.

Code
this.bookings = new Array();


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


Code
for (booking in this.bookings)


in the first and third, you're treating it like an array, but in the second you're treating it like a hashtable?


Oh yeah, for that middle one I was messing around and tried to run it with brackets instead of parenthesis so see if itd work and it didnt. I changed it back to ()

I'm pretty noob at debugging. I've just been using JShint/flint.

On the other hand, when i copy/paste this code into "dev tools" in chrome, i get:

Uncaught TypeError: this.bookings is not a function
at Cinema.addBooking <anonymous>:51:7)

Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 29 2015 08:56pm
Quote (Shakti @ Jul 29 2015 10:53pm)
Oh yeah, for that middle one I was messing around and tried to run it with brackets instead of parenthesis so see if itd work and it didnt. I changed it back to ()

I'm pretty noob at debugging. I've just been using JShint/flint.

On the other hand, when i copy/paste this code into "dev tools" in chrome, i get:

Uncaught TypeError: this.bookings is not a function
at Cinema.addBooking <anonymous>:51:7)


that's because parenthesis aren't correct either. go look up a tutorial on how to add to an array.

it's good that you're using other tools! i'm not familiar with flint, but i use JSHint on my projects.
Member
Posts: 16,399
Joined: Mar 28 2009
Gold: 22.69
Jul 29 2015 09:00pm
Quote (carteblanche @ Jul 29 2015 09:56pm)
that's because parenthesis aren't correct either. go look up a tutorial on how to add to an array.

it's good that you're using other tools! i'm not familiar with flint, but i use JSHint on my projects.


I like JSHint better because it tells you exactly whats wrong with which lines. JSFiddle* (was a typo in previous post) puts a red dot next to lines that you need to look at but doesnt tell you anything else
Member
Posts: 16,399
Joined: Mar 28 2009
Gold: 22.69
Aug 2 2015 07:16pm
still havent figured out why this isnt running lol >.>
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Aug 2 2015 07:19pm
Quote (Shakti @ Aug 2 2015 09:16pm)
still havent figured out why this isnt running lol >.>


what efforts have you made to trouble shoot it?
Member
Posts: 24,551
Joined: Sep 10 2004
Gold: 1,000.08
Sep 5 2015 12:07am
Quote (Shakti @ Aug 2 2015 09:16pm)
still havent figured out why this isnt running lol >.>


The problem is in:

Code

Cinema.prototype.getBookingsTable = function()
{
var booking;
var bookingsTableHTML = "<table border=1>";

for (booking in this.bookings)
{
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>";
}

bookingsTableHTML += "</table>";
return bookingsTableHTML;
};


this.bookings is an array you have tried to access it with a function call instead of square brackets. It should be like this: this.bookings[booking].getWhatever();
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll