Types 01
To see the solution, enter a valid code or purchase one:
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).
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
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:
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:
To see the solution, enter a valid code or purchase one:
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:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
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:
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:
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:
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:
bob
has a property
'name'
?
To see the solution, enter a valid code or purchase one:
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:
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:
To see the solution, enter a valid code or purchase one:
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:
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:
Object.keys()
and a
for..in
loop?
To see the solution, enter a valid code or purchase one:
Create an object curtis
:
_nickname
and a value 'Fif'
.
nickname
that can get and set the property _nickname
.
To see the solution, enter a valid code or purchase one:
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:
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:
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:
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:
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:
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:
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:
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:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
To see the solution, enter a valid code or purchase one:
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:
To see the solution, enter a valid code or purchase one:
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:
To see the solution, enter a valid code or purchase one:
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:
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:
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:
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:
To see the solution, enter a valid code or purchase one:
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:
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: