More Options


#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/spencer-sandbox/3/4?curriculum_id=506324b3a7dffd00020bf661#

When I run my code, I get an error message that says, “Oops, try again. Your code threw the following error when sally’s name was checked: TypeError: undefined is not an object (evaluating ‘sally.name’)”

function Person(name,age) {
  this.name = name;
  this.age = age;
  this.species = "Homo Sapiens";
}

var sally = Person("Sally Bowles", 39);
var holden = Person("Holden Caulfield", 16); 




console.log("sally's species is " + species + " and she is " + Person.age );
console.log("holden's species is " + species + " and he is " + Person.age); 



#2

maybe you want to revisit accessing properties and accessing by property part 2, seems you have forgotten

Remember: you can always revisit exercises if you need a syntax reminder


#3

I looked back at those lessons but they did not really help me when I tried to fix my code.


#4

this is what I changed my code to, but it didn’t work.
function Person(name,age) {
this.name = name;
this.age = age;
this.species = “Homo Sapiens”;
}

function sally(name,age) {
age = 39;
name = “Sally Bowles”;
species = Person.species;
}
function holden(name,age) {
age = 16;
name = “Holden Caulfield”;
species = Person.species;
}
var age1 = sally.age;
var name1 = sally.name;
var age2 = holden.age;
var name2 = holden.name;

console.log("sally’s species is " + species + " and she is " + sally.age);
console.log("holden’s species is " + species + " and he is " + holden.age);


#5

The Person function is correct. But your sally and holden functions are not correct. Let’s look at the instructions again:

Create a new object called sally using the Person constructor.

In the Another Way to Create lesson, you learn the keyword new to create an object, i.e. a new object. Right you did something like some_variable = new Object().

Use that same strategy here using the Person class to create a new person for Sally and Holden. Don’t forget to pass in their names and ages.


#6

So you are saying I should create the sally and holden objects in a form like this; sally = new Object( )?


#7

Yes, but using the class Person.

Create a new object called sally using the Person constructor.

“Create a new object” is done with the new keyword. sally is your variable name. Person is the class from which you are creating the sally object.


#8

I’m trying, but nothing is working. Im sorry for asking so many questions. This is what I changed my code to:
function Person(name,age) {
this.name = name;
this.age = age;
this.species = “Homo Sapiens”;
}
var sally = new Object();
sally.name = “Sally Bowles”;
sally.age = 39;
sally.species = Person.species;

var holden = new Object();
holden.name = “Holden Caulfield”;
holden.age = 16;
holden.species = Person.species;

console.log("sally’s species is " + Person.species + " and she is " + sally.age);
console.log("holden’s species is " + Person.species + " and he is " + holden.age);


#9

I am really sorry for all of the questioning.


#10

Look at your Person class. Notice that it takes a name and age. Therefore, when you create a new Person object, send in the person’s name and age.

For example I was creating an object for me, then I’d do:

var tonya = new Person('Tonya', 100); // nope, of course that's not my real age

Take that pattern and adapt for your exercise.

Tip: You don’t have to assign the species, as it’s automatically set when you create the object.


#11

Thank you so much! You have been of great help and you helped me fix my code! You were extraordinarily helpful!


#12

You are most welcome. I hope it helped you to see how to create objects. Enjoy the rest of the course.


#13

A post was split to a new topic: What is wrong with my code?


#14