Easy

Adding Methods to JavaScript Objects

Description

In JavaScript, objects can not only store data (properties) but also behavior (methods).

A method is a function that is associated with an object and can perform actions or computations using the object's properties.

In this challenge, you will create a person object that includes a method to calculate the person's age in dog years. T

he formula for calculating dog years is:

  • Dog Years = Human Years × 7

Example Object Structure:

const person = {
name: "Alice",
age: 25,
calculateDogYears: age * 7;
};

Your Task:

Complete the function createPerson, which takes a name and age as parameters and returns a person object with the following properties and methods:

  • name: The person's name (string).
  • age: The person's age (number).
  • calculateDogYears: A method that calculates and returns the person's age in dog years.

Example Usage:

const person1 = createPerson("John", 30); console.log(person1.calculateDogYears()); // Expected output: 210
const person2 = createPerson("Emma", 22); console.log(person2.calculateDogYears()); // Expected output: 154

Constraints:

  • The name parameter will always be a string.
  • The age parameter will always be a number.
  • The calculateDogYears method must use the this keyword to access the object's age property.

window code 2Test Cases

Input:

"Emma"

Expected Output:

{
  "age": 22,
  "name": "Emma",
  "calculateDogYears": 154
}