1

I have the following js fiddle:

EDIT: http://jsfiddle.net/Godin1/z8zD5/6/

HTML:
<table align="center" class="table table-bordered" style="border-radius: 15px;">
<col width="350">
    <col width="600">
        <col width="150">
            <tr>
                <th>Destination</th>
                <th>Description</th>
                <th>Price</th>
            </tr>
            <tr>
                <td>Villa Jibacoa
                    <br>
                </td>
                <td>
                    <ul>
                        <li>255 rooms</li>
                        <li>One 9-storey building</li>
                        <li>4 restaurants</li>
                        <li>5 bars</li>
                        <li>Pool</li>
                        <li>110 and 220 volts (adaptor required)</li>
                    </ul>
                </td>
                <td>$784
                    <div style="position: relative; margin-top: 40px;">
                        <button id="first" type="button" onclick="SeatAssignment()" class="btn btn-success">Book Now!</button>
                    </div>
                </td>
            </tr>
            <tr>
                <td>Gran Caribe Puntarena
                    <br>
                </td>
                <td>
                    <ul>
                        <li>110 rooms</li>
                        <li>Bungalow-style villas</li>
                        <li>2 restaurants</li>
                        <li>2 bars</li>
                        <li>Pools</li>
                        <li>220 volts (adaptor required)</li>
                    </ul>
                </td>
                <td>$947
                    <div style="position: relative; margin-top: 40px;">
                        <button type="button" onclick="SeatAssignment()" class="btn btn-success">Book Now!</button>
                    </div>
                </td>
            </tr>
</table>

Javascript:

var SeatSection;
var seats = (11);
var firstClass = 1;
var economy = 6;
var seatselection;

seats = 0;

function SeatAssignment()
{
        seatselection = window.prompt("Please select 1 for firstclass and 2 for economy");

        if (seatselection == 1 && firstClass <=5 && seats == 0)
            {

                window.alert("You have been assigned first class seat #" + firstClass);
                seats[firstClass] = 1;
                ++ firstClass;                                    
            }
        else if (seatselection == 2 && economy <=10 && seats == 0)
        {
            window.alert("You have been assigned economy class seat #" + economy);
                seats[economy] = 1;
                ++ economy; 
        }

        else 
        {
            window.alert("Seats are full");
        }


}

I'm not sure why the "Book Now" option isn't working at the moment but basically a window pops up and you enter 1 or 2 for first class or economy seating. There will be multiple destinations to go to therefore I need a "Book Now" for every place. However, at the moment all of the buttons are going through the same function and therefore the seating is not exclusive from one plane to another. I am wondering if I have to create a different function to take care of the seating of every different flight or can I somewhere do this with only one function?

Thanks

Godin1
  • 73
  • 10

1 Answers1

1

First, to fix your fiddle, instead of:

function SeatAssignment() {...}

Assign your function to the variable like this:

SeatAssignment = function() {..}

You can read about the differences here: var functionName = function() {} vs function functionName() {}

Then, to handle multiple flights add a parameter through which you can pass in the flight/destination this button is associated with.

So your function now becomes something like:

SeatAssignment = function(flight) {
   console.log(flight);
   ... } 

then include the appropriate information as a parameter to your onclick call:

<button type="button" onclick="SeatAssignment(2)" class="btn btn-success">Book Now!</button>

Updated fiddle: http://jsfiddle.net/z8zD5/5/

Community
  • 1
  • 1
KayakDave
  • 24,456
  • 3
  • 62
  • 67
  • When I click on the first "Book Now" and enter 1 it will say I have been assigned to first class seat #1. However when I click on the second "Book Now" and enter 1 it will say I have been assigned to first class seat #2. How do I make so that when I click the second "Book Now" It will assign me to first class seat #1? – Godin1 Nov 16 '13 at 21:19
  • You only have one `firstClass` variable. You'll need one for each flight/destination- so you'll need an array of those. Then you can use `flight` as an index into that array and increment the value associated with that flight. – KayakDave Nov 16 '13 at 21:21
  • Oh okay so I need an array for every destination? – Godin1 Nov 16 '13 at 21:23
  • One array. But with entries for each destination. I updated your fiddle to give you an idea of how that'd work: http://jsfiddle.net/z8zD5/8/ Note, I created 10 flights just as an example – KayakDave Nov 16 '13 at 21:27