Complete the code to create a prototype method that returns a greeting.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return [1] + this.name;
};The prototype method should return a greeting string followed by the person's name. Using 'Hello, ' + this.name achieves this.
Complete the code to add a shared property to all objects created by the constructor.
function Car(model) {
this.model = model;
}
Car.prototype.wheels = [1];Most cars have 4 wheels, so setting wheels to 4 on the prototype shares this property with all car objects.
Fix the error in the prototype method to correctly calculate age.
function Person(birthYear) {
this.birthYear = birthYear;
}
Person.prototype.getAge = function(currentYear) {
return currentYear [1] this.birthYear;
};To calculate age, subtract the birth year from the current year.
Fill both blanks to create a prototype method that checks if a number is even.
function NumberCheck(num) {
this.num = num;
}
NumberCheck.prototype.isEven = function() {
return this.num [1] 2 [2] 0;
};The modulo operator (%) finds the remainder. If this.num % 2 === 0, the number is even.
Fill all three blanks to create a prototype method that returns a formatted description.
function Book(title, author) {
this.title = title;
this.author = author;
}
Book.prototype.getDescription = function() {
return [1] + ': ' + [2] + ' by ' + [3];
};thisThe method should return the book's title followed by the author. Using this.title and this.author accesses the object's properties.