Create an array of objects and display details of N buildings along with the total cost
Question
Create an array of objects and display details of N buildings along with the total cost
Solution
Sure, here is a step-by-step guide on how to create an array of objects and display details of N buildings along with the total cost. I'll use JavaScript for this example.
- First, let's define a Building class:
class Building {
constructor(name, cost) {
this.name = name;
this.cost = cost;
}
}
- Now, let's create an array of Building objects:
let buildings = [
new Building('Building 1', 1000),
new Building('Building 2', 2000),
new Building('Building 3', 3000),
// add more buildings as needed
];
- To display the details of each building, we can use a loop:
for (let building of buildings) {
console.log(`Name: ${building.name}, Cost: ${building.cost}`);
}
- To calculate the total cost of all buildings, we can use the
reduce
method:
let totalCost = buildings.reduce((sum, building) => sum + building.cost, 0);
console.log(`Total cost: ${
Similar Questions
Write an algorithm for predicting house prices based on features like size, location,and number of bedrooms
Write a query to find the total amount you spent in each category (Groceries, Entertainment, etc.).
in the above question asked how will i find out the insurance of building data in overhead allocation and apportionment of all other departments.
Write a query to display the number of buildings having an email_address. Give an alias name as 'count_of_buildings_with_email'.
You are trying to predict how much money you will spend on electricity if you rent an apartment with 3 other friends. What data do you need?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.