Try it out! Intro to objects (Needs Debugging)


#1


// Our person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
    return person1.age - person2.age;
};

// Make a new function, olderAge, to return the age of
// the older of two people

// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

var olderAge = function() {
    if (alice.age>billy.age){
        return alice.age;
    }else{
        return billy.age;
    }
};
console.log("The older person is " + olderAge(alice, billy));

According to the result this works. What more does this want from me???


Loop the Loop with objects (Debugg Needed)
#2

So practically this does work but for the exercise you should use person1 and person2 instead of their names.

Let me know if that solved it.


#3

I tried that, but it complained about syntax or something similar.


#4

does it look like this?

var olderAge = function(person1, person2) {
    if (person1.age < person2.age) {
    return person2.age }
    else {
        return person1.age }
}

#5

Yeah. It says it can't find variable "Person1".


#6

Oops, try again. Your code caused a ReferenceError. Make sure you've properly defined olderAge. Here's the message: ReferenceError: Can't find variable: Person1


#7

Person1 is different to person1

#8

Oh yeah. Capitalization, right?


#9

It's still complaining about it...


#10

can is see the updated code?


#11

// Our person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}

// We can make a function which takes persons as arguments
// This one computes the difference in ages between two people
var ageDifference = function(person1, person2) {
return person1.age - person2.age;
};

// Make a new function, olderAge, to return the age of
// the older of two people

// Let's bring back alice and billy to test our new function
var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

var olderAge = function() {
if (person1.age>person2.age){
return person1.age;
}else{
return person2.age;
}
};
console.log("The older person is " + olderAge(alice, billy));


#13

Oh. Okay! Thank you!


#14

Although it still gives me the same thing.


#15

Sorry I totally got that wrong, please ignore that post that was so dumb (getting languages mixed up). Please put back to semi-colons haha.

Just like the first function you need to add function parameters.


#16

Oh! Okay! And don't worry, you're fine.


#17

There we go! That worked!
Thanks! Hopefully I'll remember that for future functions :smile:


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.