Objects in Array

In the latest lesson we looked at dealing with objects as an array, in the specific example we had a family array containing members of the family with their respective details. We constructed objects using the object constructor and then ran a couple of functions on that object.

The following is my practice code using fictional rocket ships as objects. First we'll create a set of rockets ships with all their details. Then we'll compare them to see which is the fastest.

// the rocket ship object constructor

function rocketShip (name, occupants, hull, propulsion, acceleration, weapons) {
    this.name = name;
    this.occupants = occupants;
    this.hull = hull;
    this.propulsion = propulsion;
    this.acceleration = acceleration;
    this.weapons = weapons;
}

// a new array of ships containing multiple ships with they key values
var ships = new Array ();

ships[0] = new rocketShip("LUNA",4,"armorcrete","nuclear rockets",8,"nuclear warheads");
ships[1] = new rocketShip("SERENITY",5,"tnucipun","twin ion engine",4100,"infinity reactor");
ships[2] = new rocketShip("ARGUS",10,"general products","nuclear pulse",100,"poly spectrum lasers");
ships[3] = new rocketShip("AARS1",130,"unobtanium","ion drive",30,"gamma pulse");
ships[4] = new rocketShip("AURILIUS",1,"reinforced carbon","fusion",9.4,"particle beam");


// some cool ship factoids
// https://www.fatwallet.com/blog/fastest-ship-in-the-universe/
// https://www.fatwallet.com/blog/top-sci-fi-weapons/
// http://www.projectrho.com/public_html/rocket/enginelist.php

// a function to create a list of all the ship objects we created
function shipList () {
    // calculate the number of ships in our list
    var i = ships.length;
    // set the starting point for the list, position 0 in the array is item 1
    var c = 0;
    // create a while loop that runs until we have no more ships to list
    while ( i > 0) {
        console.log("Ship #: " + c);
        console.log("Name: " + ships[c].name);
        console.log("Occupants: " + ships[c].occupants + " persons.");
        console.log("Hull Construction: " + ships[c].hull);
        console.log("Propulsion System: " + ships[c].propulsion);
        console.log("Acceleration: " + ships[c].acceleration + "G");
        console.log("Primary Weapon: " + ships[c].weapons);
        console.log("--- --- ---");
        // count down the the number of ships
        i--;
        // count up the list of ships
        c++;
    }
}

// execute the shipList function
shipList();

// a function to compare the acceleration speeds of two selected ships
function fastest (ship1, ship2) {
    // compare the speeds of the ships
    if (ship1.acceleration > ship2.acceleration) {
        console.log (ship1.name + " accelerates faster than " + ship2.name + " by " + (ship1.acceleration - ship2.acceleration) + "Gs.");
    }
    // if the above fails the opposite is true
    else {
        console.log (ship2.name + " is the clear winner at " + ship2.acceleration + "Gs; " + (ship2.acceleration - ship1.acceleration) + "Gs faster than the " + ship1.name + ".");
    }
}

// I'm not exactly sure how to feed the ships name in as a parameter, as opposed to the number in the array.
// Execute the fastest function with input from the user.
// Not sure yet how to display the list before running this function.
fastest(ships[prompt("Enter the number of the first ship you'd like to compare")], ships[prompt("Enter the number of the second ship you'd like to compare")]);

That was fun. Now back to work...