Astrolabs JavaScript Practice Assignment

Data

            
        var house = {
            areas: [{
                livingRoom: {
                    items: ['tv','sofa']
                },
                bedroomOne: {
                    items: ['bed','washing machine'],
                    windows: 3
                },
                bedroomTwo:{
                    items: ['bed','bed','desk'],
                    windows: 4
                },
                kitchen:{
                    items: ['fridge','broken chair']
                }
            }],
            garden: [true, 'Red Rose'],
            garage: {
                car: {
                    color: 'red',
                    wheels: 4,
                    honk: ()=>{alert("Beep")}
                }
            }
        }            
            
        

Questions

Please check the JS Console for the output to the following questions

  1. Find total number of areas in the house:
  2.                 
                        let houseAreas = Object.keys(house.areas[0]).length;
                        console.log(houseAreas);
                    
                
  3. Add a dining table to the living room:
  4.                 
                        house.areas[0].livingRoom.items.push("dining table");
                        console.log(house.areas[0].livingRoom.items);
                    
                
  5. Add a stove to the kitchen:
  6.                 
                        house.areas[0].kitchen.items.push("stove");
                        console.log(house.areas[0].kitchen.items);
                    
                
  7. Remove the washing machine from bedroomOne:
  8.                 
                        house.areas[0].bedroomOne.items.pop();
                        console.log(house.areas[0].bedroomOne.items);
                    
                
  9. Find the total number of beds in all rooms:
  10.                 
                        let rooms = Object.keys(house.areas[0]);
                        let beds = 0;
    
                        for (let i = 0; i < rooms.length; i++) {
                            let roomItems = house.areas[0][rooms[i]]["items"];
                            for (let j = 0; j < roomItems.length; j++) {
                                if(roomItems[j] == "bed") {
                                    beds++
                                }
                            }     
                        };
    
                        console.log(beds);
    
                    
                
  11. Change the color of the car to blue:
  12.                 
                        house.garage.car.color = "blue";
                        console.log(house.garage.car.color);
                    
                
  13. Add a another car to the garage with a honk function:
  14.                 
                        house.garage.car2 = {
                            color: 'green',
                            wheels: 4,
                            honk: ()=>{alert("Beep")}
                        };
                        console.log(house.garage);
                    
                
  15. Make the new car honk:
  16.                 
                        house.garage.car2.honk();
                    
                
  17. Change the 'broken chair' in the kitchen to 'new chair':
  18.                 
                        house.areas[0].kitchen.items.splice(1,1,"new chair");
                        console.log(house.areas[0].kitchen.items);
                    
                
  19. If the house has a garden, console.log the name of the flower:
  20.                 
                        if (house.garden[0]) {
                            console.log(house.garden[1]);
                        };