29/33 Try it Out!


#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
var olderAge=function(Person1,Person2) {
if(ageDifference<=0){
return Person2.age;}
else {
return Person1.age;}
};

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

console.log("The older person is " + olderAge(alice, billy));

Hello, this is my code. I wamt to use 'ageDifference' in my function but somehow i get this error:
Oops, try again. It looks like your olderAge function fails when the second Person is older! It should return the age of the older person


#2

Solved it! The working function should look like:

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


#3
if(ageDifference<=0){

this idea would have worked as well if you'd just it correct:

if(ageDifference(Person1,Person2)<=0){

#4

@microplayer79697,
Leaving the code in your Original post as is,
you would only had to complete the olderAge-function
with the call to your ageDifference-function like

var olderAge=function(Person1,Person2) {
   if(ageDifference(Person1,Person2)<=0){
        return Person2.age;}
   else {
        return Person1.age;
   }
};

#5

hey @leonhard_wettengmx_n,
i really benefit from your methods of approaching issues. thank you so much for your contributions. it has really helped.
do you mind dropping of your email?


#6

i don't understand why < I need to do <=0

f(ageDifference) {
person1.age > person2.age
return person1.age;
}
else{
return person2.age;
};

};

I know if this is like this it will always return the first person
but I don't understand why

if person 1 is older then person 2 return person 1 if not return person2 this how I understand how this works.


#7

// 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
function olderAge(person1, person2){

if(ageDifference) {
person1.age > person2.age ;
 return person1.age;
}
else{

   return person2.age;
    };

};

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

console.log("The older person is " + olderAge(alice, billy));


#8

Ok @pyrunner31586 can you clarify what your trying to ask about? And also improve your formatting on your code by just entering three back ticks, and then hitting enter twice, ending it with another three back ticks.. Right your code between the back ticks.. And then like i said clarify what your talking about, dont just spam code.. :wink:


#9

Oops, try again. It looks like your olderAge function fails when the second Person is older! It should return the age of the older person

dont know how to make it if person 2 is older


#10

Problem Code from @pyrunner31586 :

// 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
function olderAge(person1, person2) {

    if (ageDifference) {
        person1.age > person2.age;
        return person1.age;
    } else {

        return person2.age;
    };

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

console.log("The older person is " + olderAge(alice, billy));

#11

Ok so basically @pyrunner31586 your problem is here:

You are checking if person1.age is greater than so it should be like this:

if (person1.age > person2.age) {
        return person1.age;
    } else {
        return person2.age;
    };

#12

ahhh ok I think I been doing this for too long. tonight,

Big thanks for the help. :smiley:


#13

Remember there is never too much :stuck_out_tongue:


#14

Thanks this is more simple.


#15

Your syntax is a little off. Waaaayyyy too many curly braces.

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


#16

My code is as below:

var olderAge = function(person1, person2){
if(alice > billy){
return alice.age;
} else {
return billy.age;
}

The result is :
The older person is 25

This is not right, yet it was flagged as correct.
This is the only way i could get pass this stage.
Please help clarify.


#17

Here is a working sample without the IF statement:

function olderAge(person1, person2){
return Math.max(person1.age, person2.age);
}


#18

IMPROVING CODE - **MAKING IT WITH ARRAYS ** :slight_smile:
// 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
var olderAge = function (person1,person2){
if (person1.age > person2.age){
return person1.age }
else if (person1.age === person2.age){
return person1.age}
else {
return person2.age}
};

// Let's make an array with the names
var names = new Array ();
// inside the arrays let's use 'prompt' s we can type names and ages we want
names [0] = new Person(prompt("tell the first name"),prompt("tell me the person's age"));
names [1] = new Person(prompt("tell the second name"),prompt("tell me the second person's age"));

console.log("The older person is " + olderAge(names [0], names [1]));


#19

Oh my gosh. I was also stuck on this one for a while. Turns out, it was such a minor mistake.

My code at first used 'console.log' to print out the older person's age. Darn. It should have used 'return'.

Anyway, this code works:

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


#20

yup @kate-blake we have the same coding lol

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