0
0
Javascriptprogramming~10 mins

Why prototypes are needed in Javascript - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a prototype method that returns a greeting.

Javascript
function Person(name) {
  this.name = name;
}
Person.prototype.greet = function() {
  return [1] + this.name;
};
Drag options to blanks, or click blank then click option'
A'Hello, '
Bthis.name + 'Hello'
C'Hi ' + this.age
D'Goodbye, '
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong property like this.age instead of this.name
Putting the name before the greeting
2fill in blank
medium

Complete the code to add a shared property to all objects created by the constructor.

Javascript
function Car(model) {
  this.model = model;
}
Car.prototype.wheels = [1];
Drag options to blanks, or click blank then click option'
A0
B2
C4
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Setting wheels to 2 or 0 which is incorrect for cars
Leaving wheels undefined
3fill in blank
hard

Fix the error in the prototype method to correctly calculate age.

Javascript
function Person(birthYear) {
  this.birthYear = birthYear;
}
Person.prototype.getAge = function(currentYear) {
  return currentYear [1] this.birthYear;
};
Drag options to blanks, or click blank then click option'
A+
B*
C/
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication which gives wrong results
Using division which is incorrect here
4fill in blank
hard

Fill both blanks to create a prototype method that checks if a number is even.

Javascript
function NumberCheck(num) {
  this.num = num;
}
NumberCheck.prototype.isEven = function() {
  return this.num [1] 2 [2] 0;
};
Drag options to blanks, or click blank then click option'
A%
B===
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ===
Using == which is less strict than ===
5fill in blank
hard

Fill all three blanks to create a prototype method that returns a formatted description.

Javascript
function Book(title, author) {
  this.title = title;
  this.author = author;
}
Book.prototype.getDescription = function() {
  return [1] + ': ' + [2] + ' by ' + [3];
};
Drag options to blanks, or click blank then click option'
Athis.title
Bthis.author
Ctitle
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without this
Mixing up title and author