Javascript - Calculating age difference

I’m not sure why it is not listing the age difference, this is my code below

var youngest = {
  name: 'Maya',
  age: 13
};

var oldest = {
  name: 'Joy',
  age: 83
};

function ageDifference (youngest, oldest) {
  console.log(youngest);
  console.log(oldest);
  return youngest - oldest;
};

ageDifference(youngest, oldest);**

what do you mean by “listing the age difference”?

anyway, your code can’t work

note that the values passed in are objects, you can’t do a subtraction with objects

you need to change your approach

I’m doing a JS challenge referenced from this website: https://node-girls.gitbook.io/beginners-javascript/challenges/challenge-1-age-difference

I changed the parameters and used x and y instead.

Is this correct? It seemed to have worked.

The challenge doesn’t ask you to give values to the ageDifference function by yourself. You have to pass in youngest and oldest objects. When you pass in these objects, use dot notation or bracket notation to access the age property value of the objects and subtract those values to get the answer in the ageDifference function.

Do I still need to use the function?

I’m unsure what to put as the parameters, as I can’t use oldest/youngest…

your issue is here, you can’t do this, because the values you pass in are the objects youngest and oldest. you can’t do a subtraction with objects. you need to change that line so that you are using the numbers that are present inside the objects

This looks better

var youngest = {
  name: 'Maya',
  age: 13
};

var oldest = {
  name: 'Joy',
  age: 83
};

function ageDifference (oldest, youngest) {
  console.log(oldest);
  console.log(youngest);
  return 83 - 13;
};

ageDifference(oldest, youngest)

not really… you are not using the function parameters

you should never use hard coded values in place of values from the function parameters

you got an hint by an other user on how to do that

what happens if there is an object called evenYounger and you want to use that for calculations?

var evenYounger = {
   name: "Pier",
   age: 5
}

ageDifference(youngest, evenYounger);
ageDifference(oldest, evenYounger);

with your code the two function calls both return 70, instead the first should return 8 and the other 78

var youngest = {
  name: 'Maya',
  age: 13
};

var oldest = {
  name: 'Joy',
  age: 83
};

function ageDifference (oldest, youngest) {
  console.log(oldest);
  console.log(youngest);
  return oldest.age - youngest.age;
};

ageDifference(oldest, youngest)
2 Likes

Thank you for trying to allow me to think rather than receiving the answer. I’m very new to Javascript and have been learning HTML and CSS and finding it quite overwhelming as it is very different.

yes, it is very different!

here in the forum we try to give hints instead of the direct solution so that people can learn!

good job in solving the algorithm!

Thank you!

Would you know what type of real life example you’d use this function on a website?

I don’t know any website that deal with age differences, so no

but often when you start you are just doing simple things, to learn how things work. in real life situations the things are a bit more complex
most times users are stored in database as objects, and functions and methods work with specific properties

in a real life situation the objects would most likely have dozens of properties, but the way in which objects are manipulated doesn’t change

So glad you solved it!

You can normally calculate the age difference without passing the parameters to the function:

var youngest = {
  name: 'Maya',
  age: 13
};

var oldest = {
  name: 'Joy',
  age: 83
};

console.log(oldest.age - youngest.age);

Another method would be to pass the age to the function instead of the object:

var youngest = {
  name: 'Maya',
  age: 13
};

var oldest = {
  name: 'Joy',
  age: 83
};

function ageDifference(oldage, youngage) {
    return oldage - youngage;
};

ageDifference(oldest.age, youngest.age)