/** * trainer_completed.js * * week 9 section * fall 2013 * * train dolphins */ $(document).ready(function() { // put all of the dolphins in the ocean ocean_array = []; $.each(DOLPHINS, function(index, value) { ocean_array.push(value); }); // no dolphins are in the pool to begin with pool = null; // draw the ocean draw_ocean(); // train a dolphin when its picture is clicked $("#ocean").on("click", "td img", function() { train($(this).data("index")); }); // set a dolphin free when the button is clicked $("#pool").on("click", "input", function() { set_free($(this).data("index")); }); }); /** * draws the ocean */ function draw_ocean() { // start HTML string var html = "
"; } else { html += " | "; } }); // end HTML string html += " |
Now training " + pool.name + "
"; html += "Type: " + pool.type + "
"; html += "Genus: " + pool.genus + "
"; html += "Length: " + pool.length + " feet
"; html += "Weight: " + pool.weight + " pounds
"; html += ""; } // print out HTML string $("#pool").html(html); } /** * moves a dolphin from the ocean to the pool */ function train(index) { // only one dolphin can be trained at a time if (pool != null) { alert("You're already training a dolphin!"); return; } // remove chosen dolphin from the ocean and put in the pool pool = ocean_array[index]; pool.index = index; ocean_array[index] = null; // draw the ocean and the pool draw_ocean(); draw_pool(); } /** * moves a dolphin from the pool to the ocean. */ function set_free(index) { // remove the dolphin from the pool and put in the ocean ocean_array[index] = pool; pool = null; // draw the ocean and the pool draw_ocean(); draw_pool(); }