Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The constructor needs a parameter name to assign it to the object.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
To create a new object, pass the name string to the constructor.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The constructor parameter should be type to assign it to this.type.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameters that do not match the property names.
Using unrelated parameter names like 'model' or 'color'.
✗ Incorrect
The constructor needs parameters make and year to assign to the object properties.
5fill in blank
hardFill 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'
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.
✗ Incorrect
The constructor parameters are title and author. The method uses this.title to return the description.