3

For my CS class, I am required to make a "shopping cart" website for buying airline tickets. On this website, you can select a start location and an end location and add a ticket to your shopping cart. Currently, my available flights are defined by a multidimensional object:

    <script language="JavaScript">

        var flights = [
            {from: "Toronto, ON", to: "Detroit, MI", price: "250"},
            {from: "Toronto, ON", to: "Los Angeles, CA", price: "300"},
            {from: "Detroit, MI", to: "Toronto, ON", price: "250"},
            {from: "Detroit, MI", to: "Austin, TX", price: "350"},
            {from: "Detroit, MI", to: "Los Angeles, CA", price: "400"},
            {from: "Austin, TX", to: "Los Angeles, CA", price: "275"},
            {from: "Austin, TX", to: "Detroit, MI", price: "350"},
            {from: "Los Angeles, CA", to: "Austin, TX", price: "275"},
            {from: "Los Angeles, CA", to: "Detroit, MI", price: "400"},
            {from: "Los Angeles, CA", to: "Toronto, ON", price: "300"},
            {from: "Los Angeles, CA", to: "Sydney, AU", price: "1000"},
            {from: "Sydney, AU", to: "Los Angeles, CA", price: "1000"}
        ];


        function findFlights() {
            var total = 0;
            var toString = document.getElementById("to");
            var fromString = document.getElementById("from");
            var to = toString.value;
            var from = fromString.value;
            flights.filter(function (flights) {
                if (flights.from === from && flights.to === to) {
                    var current = parseInt(flights.price);
                    total = total + current;
                    console.log(total);
                    console.log("From " + from);
                    console.log("To " + to);
                    document.getElementById("hi").innerHTML = "blep";
                    return flights;
                }
//                else if (flights.from === from && flights.to != to) {
//                    var to = to;
//                    var newFrom = flights.to;
//                    console.log(findOne(newFrom, to));

//                    if(flights.from === newFrom && flights.to === to) {
//                        console.log(from);
//                        console.log(newFrom);
//                        console.log(to);
//                        console.log(flights);
//                    }
//                }
            });

        }

        function findOne(from, to) {
            var toString = to;
            var fromString = from;
            var to = toString.value;
            var from = fromString.value;
            flights.filter(function (flights) {
                if (flights.from === from && flights.to === to) {
                    var current = parseInt(flights.price);
                    total = total + current;
                    console.log(total);
                    console.log("From " + from);
                    console.log("To " + to);
                    return flights;
                }
            });

        }

    </script>

Right now, finding a flight for two locations connected by one thing (IE: Detroit and Toronto) works fine. However, we need to be able to put together connecting flights (IE: Detroit to Sydney), and I am having issues manipulating the data in order to do this, because it's not actually an array. Any help would be appreciated, thank you!

Also, here is a copy of the relevant HTML:

<div id="contact-form">
    <div>
        <h1>Welcome to the Flight Reservation System!</h1>
        <h4>Please fill out the form in order to reserve your flight</h4>
    </div>
    <p id="failure">Nope</p>
    <p id="success">Your message was sent successfully. Thank you!</p>


    <form action="">
        <div>
            <label for="dateDept">
                <span class="required">Departure Date</span>
                <input id="dateDept" name="dateDept" type="date" required="required" tabindex="1"
                       autofocus="autofocus"/>
            </label>
        </div>
        <div>
            <label for="dateArr">
                <span class="required">Arrival Date</span>
                <input id="dateArr" name="dateDept" type="date" required="required" tabindex="1" autofocus="autofocus"/>
            </label>
        </div>
        <div>
            <label for="from">
                <span>Subject: </span>
                <select id="from" name="from" tabindex="4">
                    <option value="Toronto, ON">Toronto</option>
                    <option value="Detroit, MI">Detroit</option>
                    <option value="Austin, TX">Austin</option>
                    <option value="Los Angeles, CA">LA</option>
                    <option value="Sydney, AU">Sydney</option>
                </select>
            </label>
        </div>
        <div>
            <label for="to">
                <span>Subject: </span>
                <select id="to" name="to" tabindex="4">
                    <option value="Toronto, ON">Toronto</option>
                    <option value="Detroit, MI">Detroit</option>
                    <option value="Austin, TX">Austin</option>
                    <option value="Los Angeles, CA">LA</option>
                    <option value="Sydney, AU">Sydney</option>
                </select>
            </label>
        </div>
        <div>
            <button type="submit" name="Add Flight" onClick="findFlights()">Purchase Flight</button>
            <input type="button" name="Add Flight" onClick="findFlights()"></input>
        </div>
    </form>

</div>

EDIT Please note: My problem is not similar to the traveling salesman, because I am not using google maps or complicated algorithms: I am simply using a pre-made Javascript object.

  • Possible duplicate of [What is a practical solution to the Travelling Salesman prblem, using Google Maps?](http://stackoverflow.com/questions/7736674/what-is-a-practical-solution-to-the-travelling-salesman-prblem-using-google-map) – Dave May 03 '17 at 02:22
  • 1
    It sounds like you should be using some algorithms from graph theory. I don't fully understand what you're trying to accomplish, but a depth-first search would allow you to find connecting flights. – 4castle May 03 '17 at 02:23
  • Sorry if I didn't explain myself well! Basically, I have 10 possible flights. If the user wants to go from a starting location I have to an ending location I have, and there is no direct flight (Ie, it's not exactly one of the entries I have), I just need to put two of them together to make it work. The logic I have in the code should work in theory (I think), but it doesn't because this is a multidimensional object and not an array. I will look into graph theory though, thank you! –  May 03 '17 at 02:27
  • *"because it's not actually an array"* - It *is* an array. `flights` is an array of objects. As an aside, why don't you make the `price` property of each flight a number rather than a string (like `price: 250`, no quotes), and then you wouldn't need `parseInt()` when adding up prices. – nnnnnn May 03 '17 at 03:06
  • @nnnnnn. Thank you for the advice! This may sound silly, but I did not know that you could input an integer without quotes. Also, I think I misspoke-- it IS an array, but I am having trouble parsing and accessing parts of the object. –  May 03 '17 at 04:10

0 Answers0