29. Try it Out! bug


#1


https://www.codecademy.com/en/courses/spencer-sandbox/4/4?curriculum_id=506324b3a7dffd00020bf661#


Hello, I have just noticed a mistake in your course, it asks for you to return the eldest age, however when you do it says you failed. Review my code below and you can see that it will return "The older person is 30" which is what the course asks for. However it passes when it displays "The older person is 25"??


// 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(){
    if (alice.age > billy.age){
        return alice.age}
        
        else{
            
            return billy.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));


#2

here:

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

why do you have a olderAge function? You already have an age difference function which calculates the difference age between two people


#3

var olderAge = function(){
if (alice.age > billy.age){
return alice.age}

    else{

        return billy.age}

The code above is the only bit I added as the course had asked for it, however it would not let me pass unless I declared that 25 was greater than 30. I just thought I should let you know.


#4

you're right, but your olderAge function should have 2 parameters, for example person1 and person2:

var olderAge = function(person1, person2){
    // function content
}

then you have to determine who is older, person1 or person2.

then when you call the function, supply bob and alice as arguments, so this values get copied into the function parameters, so your function can then determine who is older, alice or bob


#5

I have just added the parenthesis into the function command and it still comes up with Billy being older then Alice.


#6

parentheses: (), why would you add parentheses, can i see an updated version of your code?


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

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

    else{

        return billy.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

here:

if (alice.age < billy.age){

why do you use alice and billy? Use the function parameters, so that both this function calls would work:

olderAge(alice, billy)
olderAge(billy, alice)

#9

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