29-Try it out


#1


Hi all. the error is:

Oops, try again. Your code caused the following error: TypeError: olderAge is not a function


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

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

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


#2

I suggest using your function only after you've actually created it

Alternatively, change it to a function declaration which would be created before the code starts executing


#3

I try useing the function but not work


#4

and change person1 and person2 like per1 and per2

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

olderAge(alice, billy);

but not work


#5

What do you mean by "not work"? What is it doing differently from what you want?


#6

the compiler in the lesson show me a message like this

Oops, try again. Your code caused the following error: TypeError: olderAge is not a function


#7

That would suggest that you are still calling it before having created it

Or perhaps your code doesn't run at all for some reason, in which case you'll have to run your code and see for yourself if it appears to do what it should.


#8

ok man thaks, the problem was here:
console.log("The older person is " + olderAge(alice, billy));

I just move the line above of function

/ 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(person1, person2) {
if(person1.age > person2.age){
return person1.age;
}else{
return person2.age;
}}
console.log("The older person is " + olderAge(alice, billy));


#9

function declaration:

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

function expression:

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

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked.
So @ionatan wanted to say that function expression should be created before its call.


#10

Yep, it works because first function expression was created and ONLY after that you used it.


#11

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