29. why it said it incorrect?


#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;
};

var olderAge = function (person1, person2) {
if (alice < billy) {
return billy.age;
} else {
return alice.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);

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

any 1 can explain me please why its said its incorrect while the console shows the older person that is 30 age, but i get an error that its said that its not the older 1, i dont understand please, thanks for help!


#2

Your function ignores its parameters


#3

can you explain please what is wrong?


#4

What result do you expect from this?

console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))

#5

sally i think is the code stay the same


#6

Since it complains about your function being wrong, you might want to also run that code and see what happens.


#7

its still gives me the age of 25, and this is the younger 1


#8

No, neither has age 25.

Your function ignores the input that it receives.


#9

It looks like your olderAge function fails when the first Person is older! It should return the age of the older person


#10

I don't know what you mean by that.

Again, this:

console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))

Should print 927. It should not be able to come up with 25 or 30 because that was not part of the input to the function.


#11

it doesnt print 927, it print this:"It looks like your olderAge function fails when the first Person is older! It should return the age of the older person", and here is the full code of what you said,

// 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;
};

var olderAge = function (person1, person2) {
if (sally > bob) {
return sally.age;
} else {
return bob.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 sally = new Person('sally', 927);
var bob = new Person('bob', 537);

console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))


#12

But you said yourself that it should print the age of sally. Since it does not, then that's what's incorrect, that's what you need to look at. You need to think about what your function needs to do to get the correct result, and then compare those actions with the code currently in your function.

Expected behavior:

> console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))
927
> console.log(olderAge(new Person('bob', 23), new Person('sally', 5)))
23

Actual, incorrect behavior:

> console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))
30
> console.log(olderAge(new Person('bob', 23), new Person('sally', 5)))
30

Your function just returns 30. That is useless.


#13

i still cant find what is the problem with the function, it looks correct to me


#14

Where would your function get 927 from currently?


#15

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

};

now it prints me 30 , the correct 1, but still gives the error It looks like your olderAge function fails when the first Person is older! It should return the age of the older person


#16

30 is not correct, my function call doesn't ask about any person of age 30

The two ages in question are 927 and 537:

console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))

#17

age 30 and age 25 the 30 is the older why its incorrect?


#18

Because 30 is not the larger number out of 537 and 927. 927 is the larger number out of those two.

My function call never mentions 30, so why would your function come up with that result? That has nothing to do with the input that was provided.


#19

ok with ur parameters it gives me 927 , the problem its still say the same error


#20

Then maybe you didn't fix it, maybe you just changed it.

console.log(olderAge(new Person('bob', 537), new Person('sally', 927)))
console.log(olderAge(new Person('bob', 23), new Person('sally', 5)))

That should print:

927
23

I'm guessing that you are currently printing this:

927
927

Which isn't helpful, because your function is then still only ever returning the same result.

The purpose of the function is to determine which of the two supplied person's ages is the greatest. I should be able to call your function thousands of times, getting the correct result each time, without having told you in advance what the ages are.