0
0
Javascriptprogramming~10 mins

Constructors in classes 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 define a constructor in the class.

Javascript
class Person {
  constructor([1]) {
    this.name = name;
  }
}
Drag options to blanks, or click blank then click option'
Aname
Bage
Cthis
Dconstructor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' as a parameter name.
Using 'constructor' as a parameter name.
Using a parameter unrelated to the property.
2fill in blank
medium

Complete the code to create a new instance of the class.

Javascript
const person1 = new Person([1]);
Drag options to blanks, or click blank then click option'
APerson
Bthis
Cconstructor
D'John'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of a string.
Passing 'constructor' or 'this' instead of a string.
Not passing any argument.
3fill in blank
hard

Fix the error in the constructor parameter to correctly assign the property.

Javascript
class Animal {
  constructor([1]) {
    this.type = type;
  }
}
Drag options to blanks, or click blank then click option'
Athis.type
Btype
CanimalType
Dtype()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this.type' as a parameter.
Using a function call 'type()' as a parameter.
Using a different parameter name without updating the assignment.
4fill in blank
hard

Fill both blanks to create a class with a constructor that sets two properties.

Javascript
class Car {
  constructor([1], [2]) {
    this.make = make;
    this.year = year;
  }
}
Drag options to blanks, or click blank then click option'
Amake
Bmodel
Cyear
Dcolor
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameters that do not match the property names.
Using unrelated parameter names like 'model' or 'color'.
5fill in blank
hard

Fill all three blanks to create a class with a constructor and a method that returns a description.

Javascript
class Book {
  constructor([1], [2]) {
    this.title = title;
    this.author = author;
  }

  getDescription() {
    return `$[3] by ${this.author}`;
  }
}
Drag options to blanks, or click blank then click option'
Atitle
Bauthor
Cthis.title
Dtitle()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'title()' instead of 'this.title' in the method.
Using wrong parameter names in the constructor.
Not using 'this' to access object properties.