28/33 Passing Objects into Functions. Question


#1

Hi.
Can someone please tell me whats the difference between submit
this:
var diff = ageDifference(alice,billy); {
console.log(alice.age - billy.age);
}
and this:
var diff = ageDifference(alice,billy); {
console.log(ageDifference);
}


#2

Your curly braces are awfully out of place, if you mean for those to be function definitions, then you'll have to go look at some examples for how to do that. You are currently calling a function, not defining one.

If you meant for those to be function calls, then as said, those curly braces are out of place, they should not be there.

As for the difference, read what you wrote! Or be more specific about what's odd there.


#3

just write this code:

var diff = ageDifference(alice, billy);


#4

because in the lesson we already have return written in our function there is no need to display the output to the console window using console.log();

var ageDifference = function(person1, person2) {
return person1.age - person2.age;
}

// so the calculation that the ageDiffererence function performed will then be stored in the variable named diff and the result will be printed to the console windows as per the ageDifference function return code instructions:

var diff = ageDifference(alice, billy);

when you write the code this way:

var diff = ageDifference(alice,billy); {
console.log(ageDifference);
}

the output to the console window is not going to be what you might expect to see (the same output result) instead the console window output will just display:

[Function]

I don't think an error will show as the code is performing the task you instruct but the result you hoped to see will not be the actual calculated age difference between person 1 and person 2


#5

ye it gives me [Function]
yes thats my question, why do we have to do the calculation if its already stored?
If is not the age difference what is it then?


#6

@fatmario

I think you are correct in thinking we don't need to use some of the code if we don't want to display in the console window two lines of code.

line 1 with the return output as per the instruction from the function
line 2 with the console.log(ageDifference) output that does not show the result but affirms that ageDifference is indeed a function.

Like you mention, because the ageDifference function has been coded to return the result of the calculation between two people, I think re- entering the actual calculation (alice.age - billy.age); into the console.log ( ); would render it a defunct way of writing that line of code and defeat the purpose of the actual ageDifference function.

var diff = ageDifference(alice,billy); {
console.log(alice.age - billy.age);
}

I think the second example you list is indeed highlighting a way to call the ageDifference function:

// and this:
var diff = ageDifference(alice,billy); {
console.log(ageDifference);
}

That said,what I opted to do was to actually call the result of the calculation made via the function ageDifference through the stored variable named diff instead

var diff = ageDifference(alice,billy); {
console.log(diff);
}

Again, lack of sufficient knowledge dictates I can't say if this is correct or the better way to write the code - its just it seemed to me to flow better when the code was tweaked to display the result via the** diff** variable probably because, like we both mention, having the output from the code just display to the console window [Function] doesn't really help much other than it is the computers' way of affirming to us it has executed the code and verifies that (ageDifference) is indeed a function.

**// We can call ageDifference just as we might call any other function. **
It will take two parameters - the names of our two objects.

var diff = ageDifference(alice,billy); {
console.log(ageDifference);
}

Anyway, I ended up changing the code slightly as I wanted to see the result and determine whether the code was written properly so it would calculate the difference in years between person1 and person2.

here's the code with the notes from the lesson added in so I could refer back to my notes as needed:

var ageDifference = function(person1, person2) {
   
    return person1.age - person2.age;
}
 
// To make a new NAME with the constructor we use syntax:
// var name = new Person("nameOfPerson", ageOfPerson);

var alice = new Person("Alice", 30);
var billy = new Person("Billy", 25);

**// We can call ageDifference just as we might call any other function. **
**It will take two parameters - the names of our two objects.**

    var diff = ageDifference(alice,billy); {
console.log(diff);
}
*Output to console window:  5

So, like I say, I just amended my code (same as the second example you provide in your query) to get the result I hoped to see actually displayed in the console window (instead of just seeing [Function] because I already knew ageDifference was a function! so what good was that! Because I am still too new to be able to grasp everything that is being taught I am glad we have this forum to fall back on. I did not see any other listings in the forum where they had amended the code like I did but I guess that is probably because it was not necessary in order to pass the task we were given but since completing that unit it may be that someone else has posted amended slightly tweaked code to get the same desired result too. Nonetheless, whether its right or wrong, I still think opting to tweak the code till I got the desired result if only for piece of mind was a better choice lol. Hi. Can someone please tell me whats the difference between submit this:

var diff = ageDifference(alice,billy); {
// actual calculation is in the ageDifference function so this repeats the task
console.log(alice.age - billy.age);
}

and this:

var diff = ageDifference(alice,billy); {
// I think the console.log( ) should read console.log(diff) 
// as that displays the calculated age difference result 
console.log(ageDifference);
}

#7

var diff = ageDifference(alice,billy); //here you store your result in the global variable "diff"

to print your result you will then have to :

console.log(diff); //since your result is stored in the "diff" variable you will have to print that one

(But i think in this exercise is no need to make a console.log ...)


#9

Thanks!! :slightly_smiling:


#10

Thanks ye that kinda makes sense!
But how did you do it without making console.log??


#11

I think @jonesnbt means that printing is optional as far as the submission test is concerned


#12

the solution is in the 4th post


#13

var diff = ageDifference(alice, billy);{
console.log(diff);
}