Listing 7.03 - A function to create planets
JS Bin
var buildPlanet;
var getPlanetInfo;
var planet1;
var planet2;
buildPlanet = function (name, position, type, radius, rank) {
return {
name: name,
position: position,
type: type,
radius: radius,
sizeRank: rank
};
};
getPlanetInfo = function (planet) {
return planet.name.toUpperCase() + ": planet " + planet.position;
};
planet1 = buildPlanet("Jupiter", 5, "Gas Giant", 69911, 1);
planet2 = buildPlanet("Neptune", 8, "Ice Giant", 24622, 4);
console.log(getPlanetInfo(planet1));
console.log(getPlanetInfo(planet2));
Listing 7.03 - A function to create planets - Task 1
- Write a similar program to create and display objects that represent cars.
var buildCar;
var getCarInfo;
var car1;
var car2;
buildCar = function (make, model, color) {
return {
make: make,
model: model,
color: color
};
};
getCarInfo = function (car) {
return car.color + " " + car.make + " " + car.model;
};
car1 = buildCar("Ford", "Focus", "Blue");
car2 = buildCar("Aston Martin", "DB11", "Silver");
console.log(getCarInfo(car1));
console.log(getCarInfo(car2));