Home > Enterprise >  How do I add a string to an array of a constructor created object it was called on?
How do I add a string to an array of a constructor created object it was called on?

Time:01-12

Declare a function Dog that creates a new instance of a Dog object when invoked with the new keyword. Each Dog object should have a name property and a breed property, both strings which are passed in as arguments when calling the Dog function. It should also have a property called tricks, set to an array representing all the tricks that dog knows. When a new object is initiated, tricks should be empty.

All of your Dog objects must also have access to two methods that are stored on the constructor's prototype:

The first method, learnTrick, should take in a string as an argument and add that string to the tricks array of the particular Dog object it was called on.

The second method, performTrick, should also take a string as an argument. It should check to see if that string is in the tricks array belonging to the Dog instance it was called on; if so, it should log the string 'name performed trick!' If not, log the string 'name doesn't know that trick.

I tried pushing the string on to the created Constructor Array, but I might be incorrectly using dot notation.

   function Dog(name, breed) {
    this.name = name;
    this.breed = breed;
    this.tricks = [];
}

    Dog.prototype = {
    constructor: Dog,
    learnTrick: function (string1) {
        if (Dog.hasOwnProperty(string1) === false) {
            Dog.tricks = [string1];              //<---- I believe the problem lies here
            console.log(Dog.tricks);
        } else {
            tricks.push(string1)
            // console.log(tricks)
        }
    },
    performTrick: function (string2) {
        for (let property in Dog) {
            if (Dog.hasOwnProperty(string2)) {
                console.log(this.name   ' performed trick!');
            } else {
                console.log(this.name   ' doesn\'t know that trick.')
            }
        }
    }
}

    const fido = new Dog('Fido', 'poodle');

// Uncomment these lines to check your work!
    fido.learnTrick('fetch');
    fido.performTrick('fetch'); // should log 'Fido performed fetch!'
    fido.performTrick('sit'); // should log 'Fido doesn't know that trick.'`

CodePudding user response:

You overcomplicated things quite a lot. Javascript has class syntax, all this prototype fiddling is not necessary. Here's what your code should look like:

class Dog {
    constructor(name, breed) {
        this.name = name
        this.breed = breed
        this.tricks = []
    }

    learnTrick(trick) {
        this.tricks.push(trick)
    }

    performTrick(trick) {
        if (this.tricks.includes(trick)) {
            console.log(`${this.name} performed ${trick}!`)
        } else {
            console.log(`${this.name} doesn't know how to ${trick}...`)
        }
    }
}

//

const fido = new Dog('Fido', 'poodle');

fido.learnTrick('fetch');
fido.performTrick('fetch');
fido.performTrick('sit');

  • Related