0
0
Javascriptprogramming~10 mins

Class syntax 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 class named Person.

Javascript
class [1] {
  constructor(name) {
    this.name = name;
  }
}
Drag options to blanks, or click blank then click option'
APerson
Bperson
CpersonClass
DMyClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase class names like 'person' which is not a convention.
Using unrelated names like 'MyClass' or 'personClass'.
2fill in blank
medium

Complete the code to add a method named greet that returns a greeting string.

Javascript
class Person {
  constructor(name) {
    this.name = name;
  }
  [1]() {
    return `Hello, my name is ${this.name}`;
  }
}
Drag options to blanks, or click blank then click option'
Agreeting
BsayHello
Chello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names like 'sayHello' or 'hello' which do not match the instruction.
Forgetting to define the method inside the class.
3fill in blank
hard

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

Javascript
class Person {
  constructor(name) {
    this.[1] = name;
  }
}
Drag options to blanks, or click blank then click option'
Anmae
Bname
Cnam
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Using capitalized 'Name' which is different from the parameter.
Typos like 'nam' or 'nmae' cause errors.
4fill in blank
hard

Fill both blanks to create an instance of Person and call the greet method.

Javascript
const person = new [1]('Alice');
console.log(person.[2]());
Drag options to blanks, or click blank then click option'
APerson
Bgreet
CsayHello
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class names like 'person' (lowercase).
Calling a method that does not exist like 'sayHello'.
5fill in blank
hard

Fill all three blanks to add an age property, a method to get age, and create an instance with age.

Javascript
class Person {
  constructor(name, [1]) {
    this.name = name;
    this.[2] = [3];
  }
  getAge() {
    return this.age;
  }
}
Drag options to blanks, or click blank then click option'
Aage
Cyears
DageValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names like 'years' or 'ageValue' causing confusion.
Not matching the parameter and property names.