0

With the code given below, I'm trying to output the value and type of choc and I'm getting undefined for type and milk for chocolate. Can someone please help me understand how to output the type? I've been working on this for awhile and it's not clicking to me. Thanks!

// we set up a base class
function Candy() {
    this.sweet = true;
}

// create a "Chocolate" class with a "type" argument
Chocolate = function(type){
    this.type = type;
};

// say that Chocolate inherits from Candy

Chocolate.prototype = new Candy();

// create a "choc" object using the Chocolate constructor 
// that has a "type" of "milk"

var choc = new Object();
choc.type = "milk";

// print the sweet and type properties of choc
console.log(choc.sweet);
console.log(choc.type);

//////this is what I changed it to and still doesnt work//////////

// we set up a base class
function Candy() {
    this.sweet = true;
}

// create a "Chocolate" class with a "type" argument
Chocolate = function(type){
    this.type = type;
};

// say that Chocolate inherits from Candy

Chocolate.prototype = new Candy();

// create a "choc" object using the Chocolate constructor 
// that has a "type" of "milk"

var choc = new Chocolate();
choc.type = "milk";

// print the sweet and type properties of choc
console.log(choc.sweet);
console.log(choc.type);
5
  • Why do you use the Object constructor but say you would use the Chocolate constructor? Commented Aug 24, 2012 at 16:15
  • 2
    You say "print the sweet property", but you code .value. Commented Aug 24, 2012 at 16:16
  • I changed it to Chocolate constructor on codecademy but still doesnt work. Commented Aug 24, 2012 at 16:18
  • I ran the second half (in Chrome) of what you posted, and that works for me. I get outputs of true, and then milk. Commented Aug 24, 2012 at 16:32
  • You should accept the correct answer to your question, if it worked for you. Commented Mar 24, 2013 at 22:09

1 Answer 1

4

Look at the last four lines of your code (it does not use anything from above):

// create a "choc" object using the Chocolate constructor 
// that has a "type" of "milk"

var choc = new Object();
choc.type = "milk";

// print the sweet and type properties of choc
console.log(choc.value);
console.log(choc.type);

Neither did you create a Chocolate object, nor did you print the sweet property (therefore getting undefined for value).

Instead, use

var choc = new Chocolate("milk");
console.log(choc.sweet); // true
console.log(choc.type); // "milk"

Your updated code works for me.

Sign up to request clarification or add additional context in comments.

3 Comments

you are correct, while you were typing that I did try it. It still has the same results.
@MrNart: Your edited code works perfectly fine for me. Do you get any error in the console?
Thanks guys, I just refreshed and it worked. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.