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
  • Explanation
  1. 04-Objects
  2. Solutions
  3. Family Challenge

Pet

What family would be complete without a pet?

Completed Code

function Pet(firstName, lastName, gender, age, birthday, animal, breed, color) {
    Person.call(this, firstName, lastName, gender, age, birthday)

    this.animal = animal;
    this.breed = breed;
    this.color = color;
    this.good = function () {
        if (this.pronoun().toLowerCase() == "he") {
            return "boy";
        } else if (this.pronoun().toLowerCase() == "she") {
            return "girl";
        } else {
            return this.animal;
        }
    }
}
Pet.prototype = Object.create(Person.prototype);
Pet.prototype.constructor = Pet;

Pet.prototype.description = function () {
    return ("My " + this.animal + ", " + this.firstName + ", is the world's greatest " + this.breed + ".");
};
Pet.prototype.bday = function () {
    return ("Our " + this.color + " " + this.breed + " turns " + this.nextYear + " on " + this.birthday + ".");
};
Pet.prototype.praise = function () {
    return (this.pronoun() + " is/are " + "such a good " + this.good() + "!");
};
var pet1 = new Pet('Bon-Bon', 'Jovi', 'female', 3, 'February 14', 'dog', 'Border Collie', 'black and white');
console.log(pet1);
console.log(pet1.description());
console.log(pet1.bday());
console.log(pet1.praise());

Output

Pet {
  firstName: 'Bon-Bon',
  lastName: 'Jovi',
  gender: 'female',
  age: 3,
  birthday: 'February 14',
  nextYear: 4,
  fullName: [Function],
  pronoun: [Function],
  possessive: [Function],
  ordinalSuffix: [Function],
  animal: 'dog',
  breed: 'Border Collie',
  color: 'black and white',
  good: [Function] }

My dog, Bon-Bon, is the world's greatest Border Collie.
Our black and white Border Collie turns 4 on February 14.
She is/are such a good girl!

Explanation

  1. New Properties

    • animal - is your pet a dog, a cat, fish, horse, etc?

    • breed - is your dog a boxer, your cat a siamese?

    • color - what color is/are your pets fur, scales, feathers, etc?

    • good() - a function that we'll use to output a noun based on the gender pronoun.

  2. New Functions

    • description() - displays animal, firstName, and breed.

    • bday() - displays color, breed, as well as nextYear and birthday

    • praise() - displays pronoun and the result of good().

PreviousChildNext05-Arrays

Last updated 7 years ago