Listing 7.02 - A function that adds properties to an object
JS Bin
var planet1 = { name: "Jupiter", radius: 69911 };
var calculateSizes = function (planet) {
var r = planet.radius;
planet.area = 4 * 3.142 * r * r;
planet.volume = 4 * 3.142 * r * r * r / 3;
};
var displaySizes = function (planet) {
console.log(planet.name);
console.log("surface area = " + planet.area + " square km");
console.log("volume = " + planet.volume + " cubic km");
};
calculateSizes(planet1);
displaySizes(planet1);
Listing 7.02 - A function that adds properties to an object - Tasks 1&2
- Update the calculateSizes function so it also adds a diameter property to the planet.
- Update the displaySizes function so it also displays the diameter on the console.
var planet1 = { name: "Jupiter", radius: 69911 };
var calculateSizes = function (planet) {
var r = planet.radius;
planet.area = 4 * 3.142 * r * r;
planet.volume = 4 * 3.142 * r * r * r / 3;
planet.diameter = 2 * r; // add a diameter property
};
var displaySizes = function (planet) {
console.log(planet.name);
console.log("surface area = " + planet.area + " square km");
console.log("volume = " + planet.volume + " cubic km");
// display the diameter
console.log("diameter = " + planet.diameter + " km");
};
calculateSizes(planet1);
displaySizes(planet1);