JavaScript exercises - OOP

Note: this collection of 45 exercises was inspired by Nicholas C. Zakas' book The principles of Object-oriented JavaScript. Special thanks and much gratitude to him!

Bite-sized, tested and vetted exercises only: no storytelling, no riddle, minimal use of math (basic arithmetic).

Types 01

What are the primitive types in JavaScript?

To see the solution, enter a valid code or purchase one:


Types 02

What is the specificity of the primitive types Null and Undefined?

To see the solution, enter a valid code or purchase one:


Types 03

How can we identify primitive types?

To see the solution, enter a valid code or purchase one:


Types 04

What is an object in JavaScript?

To see the solution, enter a valid code or purchase one:


Types 05

How do we create (or instantiate) objects (reference types) in JavaScript?

To see the solution, enter a valid code or purchase one:


Types 06

Primitive values do not have methods in JavaScript. What does the JavaScript engine do under the hood to make the following code work?

const name = 'Eva';
const big_eva = name.toUpperCase();
console.log(big_eva); // EVA

To see the solution, enter a valid code or purchase one:


Types 07

Primitive values do not have methods in JavaScript. What does the JavaScript engine do under the hood to make the following code work?

const name = 'Crixus';
name.last = 'The Great';
console.log(name.last) // undefined

To see the solution, enter a valid code or purchase one:


Types 08

What is the difference between primitive types and reference types?

To see the solution, enter a valid code or purchase one:


Types 09

For some built-in reference types, it is possible to create new instances (objects) either by using the constructor function or by using a literal form. Is it better to use constructors or literals?

To see the solution, enter a valid code or purchase one:


Functions 01

What is the difference between functions and other objects?

To see the solution, enter a valid code or purchase one:


Functions 02

How many function literals are there?

To see the solution, enter a valid code or purchase one:


Functions 03

Declare a function sum that can have any number of arguments and add them together. The function should return the following values:

console.log(sum()) // 0
console.log(sum(5)) // 5
console.log(sum(1, 2)) // 3
console.log(sum(1, 2, 3, 4)) // 10

To see the solution, enter a valid code or purchase one:


Functions 04

const cat = {
  species: 'cat',
  talk: function() {
    console.log(`I'm a ${this.species}.`);
  },
};

const dog = {
  species: 'dog',
};

cat.talk() // I'm a cat.

Make the dog talk like the cat, but without changing the dog object (do not add any method to the dog object).

To see the solution, enter a valid code or purchase one:


Functions 05

const flamingo = {
  species: 'flamingo',
  talk: function(salutation) {
    console.log(`${salutation}! I'm a ${this.species}.`);
  },
};

const puma = {
  species: 'puma',
};

flamingo.talk('Woo'); // Woo! I'm a flamingo.

Make the puma talk like the flamingo, but without changing the puma object (do not add any method to the puma object). The puma must say: "Yeah, I'm a puma.".

To see the solution, enter a valid code or purchase one:


Functions 06

const pelican = {
  species: 'pelican',
  talk: function(salutation, name) {
    console.log(`${salutation}! I'm ${name}. I'm a ${this.species}.`);
  },
};

const crocodile = {
  species: 'crocodile',
};

pelican.talk('Yo', 'Guru'); // Yo! I'm Guru. I'm a pelican.

Make the crocodile talk like the pelican, but without changing the crocodile object (do not add any method to the crocodile object). The crocodile must say: "Yup! I'm Lala. I'm a crocodile.".

To see the solution, enter a valid code or purchase one:


Objects 01

How can we check if an object bob has a property 'name'?

To see the solution, enter a valid code or purchase one:


Objects 02

const bob = {
  name: 'Marley',
};

console.log('name' in bob);
console.log(bob.hasOwnProperty('name'));
console.log('toString' in bob);
console.log(bob.hasOwnProperty('toString'));

What does the previous code print to the screen?

To see the solution, enter a valid code or purchase one:


Objects 03

const kodak = {
  first_name: 'Bill',
  last_name: 'Kapri',
  age: 'Unknown',
};

Delete the property age from the object kodak.

To see the solution, enter a valid code or purchase one:


Objects 04

What is the characteristic of an enumerable property?

To see the solution, enter a valid code or purchase one:


Objects 05

Why most of the time is it possible to use either Object.keys() or for..in to iterate over the own properties of an object?

To see the solution, enter a valid code or purchase one:


Objects 06

const family = {
  mother: 'Carole',
  father: 'Julien',
  child_1: 'Raphaël',
  child_2: 'Nathanaël',
  child_3: 'Noémie',
  dog: 'Polka',
  chill: function() {
    console.log('We go climbing');
  },
};

Iterate though the properties of the object family and print the name of each property to the screen.

To see the solution, enter a valid code or purchase one:


Objects 07

When iterating through the properties of an object, what is the difference between using Object.keys() and a for..in loop?

To see the solution, enter a valid code or purchase one:


Objects 08

Create an object curtis:

  1. with a data property named _nickname and a value 'Fif'.
  2. with an accessor property named nickname that can get and set the property _nickname.

To see the solution, enter a valid code or purchase one:


Objects 09

const moon = {};

Add a property temperature to the object moon that can be deleted, that cannot be changed, that can be iterated over and with a value 'Variable'.

To see the solution, enter a valid code or purchase one:


Objects 10

Create an object sun with the following characteristics:

delete sun.temperature;
console.log(sun.temperature); // 'Hot'

sun.temperature = 'Very hot';
console.log(sun.temperature); // 'Very Hot'

const keys = Object.keys(sun);
console.log(keys.length); // 0

To see the solution, enter a valid code or purchase one:


Objects 11

Create an object moon with the following characteristics:

const keys_1 = Object.keys(moon);
console.log(keys_1.length); // 1

console.log(moon.temperature); // 'variable'
moon.temperature = 'very variable';
console.log(moon.temperature); // 'variable'

delete moon._temperature;
const keys_2 = Object.keys(moon);
console.log(keys_2.length); // 0

To see the solution, enter a valid code or purchase one:


Objects 12

const cat = {};

Object.defineProperty(cat, 'name', {
  value: 'Freya',
});

delete cat.name;
console.log('name' in cat);

const keys = Object.keys(cat);
console.log(keys.length);

cat.name = 'Doja';
console.log(cat.name);

Without running the code, what is printed to the screen?

To see the solution, enter a valid code or purchase one:


Objects 13

const dog = {
  _age: 2,
};

Add a property age to the object dog so that the following code works:

const keys = Object.keys(dog);
console.log(keys.length) // 1

delete dog.age;
console.log(dog.age); // undefined

To see the solution, enter a valid code or purchase one:


Objects 14

2 attributes are specific to data properties, 2 others are specific to accessor properties, and 2 can be applied to both properties. Fill in the the first column of the following table with the right attribute names.

Attributes
Applies to
Data and accessor properties
Data and accessor properties
Data properties
Data properties
Accessor properties
Accessor properties

To see the solution, enter a valid code or purchase one:


Objects 15

const parrot_1 = {
  _color: 'green',
  get color () {
    return this._color;
  },
  set color(new_color) {
    this._color = new_color;
  }
};

const parrot_2 = {};

Define the same properties as parrot_1 on object parrot_2 by using the method Object.defineProperties().

To see the solution, enter a valid code or purchase one:


Objects 16

const bruce = {
  secret: 'batman',
};

Print to the screen the attributes of the property secret of the object bruce.

To see the solution, enter a valid code or purchase one:


Objects 17

What is a non-extensible object?

To see the solution, enter a valid code or purchase one:


Objects 18

How can you make an object non-extensible?

To see the solution, enter a valid code or purchase one:


Objects 19

What is the difference between a non-extensible object, a sealed object and a frozen object?

To see the solution, enter a valid code or purchase one:


Objects 20

Complete the following table:

Attribute Entity concerned Behavior when set to true
[[Enumerable]]
[[Configurable]]
[[Writable]]
[[Value]]
[[Get]]
[[Set]]
[[Extensible]]

To see the solution, enter a valid code or purchase one:


Constructors and prototypes 01

What is a constructor?

To see the solution, enter a valid code or purchase one:


Constructors and prototypes 02

function Dog(name) {
  this.name = name;
  return 'Rex';
}

function Cat(name) {
  this.name = name;
  return {
    name: name.toUpperCase(),
  };
}

function Cow(name) {
  this.name = name;
}

const dog = new Dog('Toby');
console.log(dog.name);

const cat = new Cat('Ramses');
console.log(cat.name);

const cow = new Cow('Moo');
console.log(cow.name);

Without running the code, what is printed to the screen?

To see the solution, enter a valid code or purchase one:


Constructors and prototypes 03

What is a prototype?

To see the solution, enter a valid code or purchase one:


Constructors and prototypes 04

function Bear(name) {
  this.name = name;
}

Bear.prototype = {
  sayName() {
    console.log(this.name);
  }
};

const baloo = new Bear('Baloo');

console.log(baloo instanceof Bear);
console.log(baloo.constructor === Bear);

Whithout running the previous code, what is printed to the screen?

To see the solution, enter a valid code or purchase one:


Inheritance 01

const movie_1 = {
  name: 'Forrest Gump',
  date: 1994,
};

const movie_2 = {
  name: 'Chicago',
  date: 2002,
};

Modifiy the objects movie_1 and movie_2 so that we can compare movies by comparing their dates.

console.log(movie_1 > movie_2); // false
console.log(movie_1 < movie_2); // true

To see the solution, enter a valid code or purchase one:


Inheritance 02

const book = {
    title: 'Atomic habits',
    author: 'James Clear',
};

console.log(`I like ${book}.`);
// 'I like James Clear's book Atomic Habits.'

Add a method to the object book so that the previous code works as expected.

To see the solution, enter a valid code or purchase one:


Inheritance 03

const basketball = {
  jump() {
    console.log('I can jump');
  },
  dribble() {
    console.log('I can dribble');
  }
}

Create an object player that inherits from the methods jump() and dribble(), without creating any constructor function.

The following code should work as expected:

player.jump(); // 'I can jump'
player.dribble(); // 'I can dribble'
console.log(player.hasOwnProperty('jump')); // false
console.log(player.hasOwnProperty('dribble')); // false

To see the solution, enter a valid code or purchase one:


Object patterns 01

What is an IIFE and what is it used for?

To see the solution, enter a valid code or purchase one:


ES5 to ES6

function Monument(name, location) {
  this.name = name;
  this.location = location;
}

Object.defineProperty(Monument, 'definition', {
  get() {
    return 'A notable construction';
  },
});

Monument.prototype.see = function() {
  console.log(`I can see the ${this.name} in ${this.location}.`);
};

const eiffel_tower = new Monument('Eiffel Tower', 'Paris');
eiffel_tower.see();
console.log('What is a monument?', Monument.definition);

Create a modernMonument class that is the equivalent of the previous code, by using the class keyword (ES6 syntax). Create a taj_mahal monument, with the name 'Taj Mahal' and the location 'India', just like eiffel_tower in the previous code.

To see the solution, enter a valid code or purchase one:


ES6 to ES5

class luxuryCar {
  constructor(brand, year) {
    this.brand = brand;
    this.year = year;
  }
  
  static get definition() {
    return 'A very expensive car.';
  }
  
  drive() {
    console.log(`I have a ${this.brand} from ${this.year}.`);
  }
}

const bentley = new luxuryCar('Bentley', 2023);
bentley.drive();
console.log('What is a luxury car?', luxuryCar.definition);

Create a es5LuxuryCar() factory function that is the equivalent of the previous code, without using the class keyword (ES6 syntax). Create a ferrari es5LuxuryCar, with the name 'Ferrari' and the year 1980, just like bentley in the previous code.

To see the solution, enter a valid code or purchase one: