Table of Contents
Back Button
Back to Chapter
Control Statements
No items found.
Function & Events
No items found.
Array and its Types
No items found.
OOP : Object Oriented Programming
No items found.
Javascript Standards
No items found.
HTML DOM
No items found.
Javascript : Advanced 1
No items found.
Javascript : Advanced 2
No items found.
Additional JavaScript
No items found.

Practice Exercise

Question

  1. Create an Object that stores country name, capital, language, population, neighbours and create a describe function using get all output all these variables.
  2. Do This same with Constructors.
  3. Do Same with Classes and reuse it for USA.

Hint

For General

Create an Object names myCountry and assign the values of country to their respective variables.

example : country : 'India'

Now create a describe function that prints the value of assigned variables in Console by using this Statement.

Call the function inside the Object.

Note: Logic is Same in All Question, you just need to use specific Syntax for each case.

Solution (Object & Get)

const myCountry = { country: 'India', capital: 'Delhi', language: 'Hindi', population: 1360, neighbours: ['Australia', 'Nepal', 'Bangladesh'], get describe(){ return(this.country + " has a population of " + this.population + " million "+ this.language +" speaking people , " + this.neighbours.length + " neighbouring countries and a capital called " + this.capital); } }; var myCountry2 = myCountry.describe; console.log(myCountry2); // Output // India has 1360 million Hindi speaking people , 3 neighbouring countries and a capital called Delhi

Object With Constructor

function myCountry(){ this.country= 'India', this.capital= 'Delhi', this.language= 'Hindi', this.population= 1360, this.neighbours= ['Australia', 'Nepal', 'Bangladesh'], this.describe = function(){ return(this.country + " has a population of " + this.population + " million "+ this.language +" speaking people , " + this.neighbours.length + " neighbouring countries and a capital called " + this.capital); } }; const myCountry2 = new myCountry(); console.log(myCountry2.describe()); // Output // India has population of 1360 million Hindi speaking people , 3 neighbouring countries and a capital called Delhi

Using Classes

class country{ constructor(name,capital,language,population,neighbours){ this.country = name; this.capital = capital; this.language = language; this.population = population; this.neighbours = neighbours; } describe(){ return(this.country + " has a population of " + this.population + " million "+ this.language +" speaking people , " + this.neighbours.length + " neighbouring countries and a capital called " + this.capital); } } let india = new country("India", "Delhi" ,"Hindi", "1360","['Australia', 'Nepal', 'Bangladesh']"); console.log(india.describe()); let usa = new country("U.S.A", "Washington, D.C." ,"English", "329","['Canada', 'Alaska', 'Mexico']"); console.log(usa.describe()); // Output // India has a population of 1360 million Hindi speaking people , 3 neighbouring countries and a capital called Delhi // U.S.A has a population of 329 million English speaking people , 30 neighbouring countries and a capital called Washington, D.C.
Contact Us
Contact Us on Whatsapp
Hi, How Can We Help You?