Javascript - Help w/ prototypes and properties?

demboiz

I'm currently writing a code for my javascript class and I've hit a dead end. The task is to make a "virtual pet app" that has several properties: hungry, ill, and lonely. There are also two prototypes (a dog and a fish) that share the main pet object. Here is my code so far:

'use strict';

// Pet Prototype
var pet = {
    name: 'Your Pet',
    hungry: true,
    ill: false
};


pet.feed = function(){
    this.hungry = false;
    return this.name + ' is full.';
};


pet.newDay = function(){
    for (var prop in this){
        if (typeof this[prop] === 'boolean'){
            this[prop] = true;
        }
    }
    return 'Good morning!';
};


pet.check = function(){
    var checkVal = '';

    for (var prop in this){
        if (typeof this[prop] === 'boolean'
            && this[prop] === true){
            checkVal += this.name + ' is ' + prop + '. ';
        } 
    }
    if (typeof this[prop] === 'boolean'
            && this[prop] === false){
            checkVal = this.name + ' is fine.';
    }
    return checkVal;
};

// Fish Prototype
var fish = Object.create(pet);


fish.clean = function(){
    this.ill = false;
    return this.name + ' likes the clean tank.';
};


// Dog Prototype
var dog = Object.create(pet);


// Lonely Property
dog.lonely = false;


dog.walk = function(){
    this.ill = false;
    return this.name + ' enjoyed the walk!';
}


dog.play = function(){
    this.lonely = false;
    return this.name + ' loves you.';
}

There is more to the code, however I think you guys get the gist by now. The problem is that my check function does not work properly...

console.log(myDog.check());      // Fido is hungry.
console.log(myDog.feed());       // Fido is full.
console.log(myDog.check());      // Fido is fine.
console.log(myFish.check());     // Wanda is hungry.
console.log(myFish.feed());      // Wanda is full.
console.log(myFish.check());     // Wanda is fine.
console.log(myDog.newDay());     // Good morning!
console.log(myFish.newDay());    // Good morning!
console.log(myDog.check());      // Fido is hungry. Fido is lonely. Fido is ill.

This is what my output is supposed to be, however this is MY output:

Fido is hungry. 
Fido is full.
(an empty string)
Wanda is hungry. 
Wanda is full.
(an empty string)
Good morning!
Good morning!
Fido is hungry. Fido is lonely. Fido is ill. 

Can someone direct me towards why my check function is not printing pet.name is fine, and is instead printing any empty string? Thanks in advance!

Barmar

It looks like your intent is to display is fine if none of the properties are true. Instead of checking this[prop], which makes no sense because the value of prop after the loop is unpredictable, just check whether checkVal has been set by the loop.

pet.check = function(){
    var checkVal = '';

    for (var prop in this){
        if (typeof this[prop] === 'boolean'
            && this[prop] === true){
            checkVal += this.name + ' is ' + prop + '. ';
        } 
    }
    if (checkVal == ''){
        checkVal = this.name + ' is fine.';
    }
    return checkVal;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Basic JavaScript object structure help accessing properties

From Dev

Spring prototypes inherit properties at runtime

From Dev

How do javascript engines set properties on objects that have prototypes with a setter for that property?

From Dev

JavaScript functions and prototypes

From Dev

Understanding of JavaScript Prototypes

From Dev

Javascript nested prototypes

From Dev

Constructor and prototypes in javascript

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and property inheritence in JavaScript

From Dev

Prototypes issue in javascript

From Dev

JavaScript prototypes: replace a function

From Dev

Javascript nested prototypes

From Dev

Safely inheriting prototypes in JavaScript

From Dev

Prototypes and usage Javascript

From Dev

Understanding how JavaScript Prototypes work

From Dev

JavaScript Inheritance with Prototypes -- 'constructor' property?

From Dev

Creating functions in function prototypes for Javascript

From Dev

Extending prototypes in Javascript - good way?

From Dev

How to avoid using "this" in Javascript prototypes

From Dev

JavaScript Inheritance with Prototypes -- 'constructor' property?

From Dev

How to avoid using "this" in Javascript prototypes

From Dev

More prototypes at once (chaining) in javascript

From Dev

using a group of functions as a prototypes in javascript

From Dev

Jasmine and Spying/Mocking - JavaScript Prototypes

From Dev

javascript extending prebuilt prototypes performance

From Dev

JavaScript - The Good Parts: Function prototypes vs Object prototypes

From Dev

Need help understanding file uploads in Python Flask, w/ and w/o Javascript + Ajax

From Dev

Homework Help w/ strings

From Dev

Rounding w/ SUMIF (HELP)

Related Related

  1. 1

    Basic JavaScript object structure help accessing properties

  2. 2

    Spring prototypes inherit properties at runtime

  3. 3

    How do javascript engines set properties on objects that have prototypes with a setter for that property?

  4. 4

    JavaScript functions and prototypes

  5. 5

    Understanding of JavaScript Prototypes

  6. 6

    Javascript nested prototypes

  7. 7

    Constructor and prototypes in javascript

  8. 8

    Safely inheriting prototypes in JavaScript

  9. 9

    Prototypes and property inheritence in JavaScript

  10. 10

    Prototypes issue in javascript

  11. 11

    JavaScript prototypes: replace a function

  12. 12

    Javascript nested prototypes

  13. 13

    Safely inheriting prototypes in JavaScript

  14. 14

    Prototypes and usage Javascript

  15. 15

    Understanding how JavaScript Prototypes work

  16. 16

    JavaScript Inheritance with Prototypes -- 'constructor' property?

  17. 17

    Creating functions in function prototypes for Javascript

  18. 18

    Extending prototypes in Javascript - good way?

  19. 19

    How to avoid using "this" in Javascript prototypes

  20. 20

    JavaScript Inheritance with Prototypes -- 'constructor' property?

  21. 21

    How to avoid using "this" in Javascript prototypes

  22. 22

    More prototypes at once (chaining) in javascript

  23. 23

    using a group of functions as a prototypes in javascript

  24. 24

    Jasmine and Spying/Mocking - JavaScript Prototypes

  25. 25

    javascript extending prebuilt prototypes performance

  26. 26

    JavaScript - The Good Parts: Function prototypes vs Object prototypes

  27. 27

    Need help understanding file uploads in Python Flask, w/ and w/o Javascript + Ajax

  28. 28

    Homework Help w/ strings

  29. 29

    Rounding w/ SUMIF (HELP)

HotTag

Archive