0
0
Javascriptprogramming~10 mins

Constructor functions in Javascript - Interactive Code Practice

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

Complete the code to create a constructor function named Person.

Javascript
function [1](name, age) {
  this.name = name;
  this.age = age;
}
Drag options to blanks, or click blank then click option'
AcreatePerson
Bperson
CPerson
DnewPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase function names for constructors.
Using names that do not describe the object.
2fill in blank
medium

Complete the code to create a new Person object named john.

Javascript
const john = new [1]('John', 30);
Drag options to blanks, or click blank then click option'
AnewPerson
BPerson
CcreatePerson
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names when calling the constructor.
Forgetting the new keyword.
3fill in blank
hard

Fix the error in the constructor function to correctly assign the age property.

Javascript
function Person(name, age) {
  this.name = name;
  this.[1] = age;
}
Drag options to blanks, or click blank then click option'
Aages
Bagee
CAge
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the property name.
Using uppercase letters incorrectly.
4fill in blank
hard

Fill both blanks to add a method greet to the Person prototype that returns a greeting string.

Javascript
Person.prototype.[1] = function() {
  return `Hello, my name is [2].`;
};
Drag options to blanks, or click blank then click option'
Agreet
BsayHello
Cthis.name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names.
Not using this to access properties.
5fill in blank
hard

Fill all three blanks to create a Person object and call its greet method, storing the result in message.

Javascript
const [1] = new Person('Alice', 25);
const [2] = [3].greet();
Drag options to blanks, or click blank then click option'
Aalice
Bmessage
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Forgetting to call the method.