JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  • Completed Code
  • Output
  1. 04-Objects
  2. Solutions
  3. Inheritance Challenge

Code

Completed Code

Add this below your existing code.

function Parent(firstName, lastName, gender, age, birthday, isMarried, occupation) {
    Person.call(this, firstName, lastName, gender, age, birthday);

    this.isMarried = isMarried;
    this.occupation = occupation;
}
Parent.prototype = Object.create(Person.prototype);
Parent.prototype.constructor = Parent;

Parent.prototype.job = function () {
    if (this.pronoun().toLowerCase() == "he" || this.pronoun().toLowerCase() == "she") {
        return (this.pronoun() + " is a/an " + this.occupation + ".");
    } else {
        return (this.pronoun() + " are a/an " + this.occupation + ".");
    }
};

var parent1 = new Parent ('Jane', 'Jovi', 'female', 29, 'May 9', true, 'coder');
console.log(parent1);
console.log(parent1.job());

Output

Parent {
  firstName: 'Jane',
  lastName: 'Jovi',
  gender: 'female',
  age: 29,
  birthday: 'May 9',
  nextYear: 30,
  fullName: [Function],
  pronoun: [Function],
  possessive: [Function],
  isMarried: true,
  occupation: 'coder' }

  She is a/an coder.
PreviousInheritance ChallengeNextExplanation

Last updated 7 years ago