Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

323
Views
Javascript, inherit attribute and method of objects

I got this javascript exercise. I made the Customer object and inherit all of the attributes from Person objects. At first, I got the error which says there is no "name" function in Customer. so I tried if I could get "firstname" from Customer object, but it also didn't work and returned the error saying there is no "customer1.firstname is not a function".

I googled and I think call function should inherit the attributes("firstname" and "lastname", also "name" method) into Customer object. I'd be glad if you help me out with this.

Thank you in advance!


Write a definition for the Customer objects. Customer has to inherit Person. In addition to the inherited attributes, Customer needs to have "id" and "spent", which represents the amount of money the customer has already used in the specific store. If a value for "spent" is not included when creating the object, its value should be 0. Additionally, the Customer objects should have a method "bonus", which checks how much money the customer has spent. If the value is over 5000, the method returns "gold", if over 1000, "silver", and if 1000 or below, "basic".

The output should be like this↓

Normal Natalia
Programmer Pete
basic
Coder Cara
silver
Goldson Gilbert
gold

Here is the prepared code which is not allowed to be edited.

function Person(firstname, lastname) {
  this.firstname = firstname;
  this.lastname = lastname;
}
Person.prototype.name = function() {return (this.lastname + " " + this.firstname);}

Here is what I have done so far.

function Customer(firstname, lastname,name,id, spent) {
    Person.call(this, firstname, lastname, name);
    
    this.id = id;
    this.spent = spent;
    
    this.bonus = function() { /* check how much the customer has spent */
        if ( this.spent >= 5000 ) {
            return "gold";
        } else if ( this.spent >= 1000 && this.spent < 5000 ) {
            return "silver";
        } else {
            return "basic";
        }
    };
}

Here is also the prepared code.

var person = new Person("Natalia", "Normal");
var customer1 = new Customer("Pete", "Programmer", 1);
var customer2 = new Customer("Cara", "Coder", 2, 1500);
var customer3 = new Customer("Gilbert", "Goldson", 3, 8000);

console.log(person.name());
console.log(customer1.firstname());
console.log(customer1.name());
console.log(customer1.bonus());
console.log(customer2.name());
console.log(customer2.bonus());
console.log(customer3.name());
console.log(customer3.bonus());
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!