0

I just started learning JavaScript and I would like to make it work . I wrote function that check your age based on the current date and your birthday information. The Function working just fine ( Yes I know that there is probably faster and clearly way to create this Function ) .

var today = new Date();
var currentDay = today.getDate();
var currentMoth = today.getMonth() + 1
var currentYear = today.getFullYear()


function birthdayCheck(birth_day, birth_month, birth_year) {
    if (currentMoth > birth_day || (birth_day <= currentDay && currentMoth >= birth_month)) {
        return (currentYear - birth_year)
    } else {
        return (currentYear - birth_year) - 1
    }
}

I want to use this function inside Object .

let birthday = {
    birthYear: 2010,
    birthDay: 3,
    birthMonth: 8,
    myAge: birthdayCheck(this.birthDay, this.birthMonth, this.birthYear),

}

console.log(birthday.myAge);

if I'm wring number insted of this.birthDay, this.birthMonth, this.birthYear its worknig fine but I don't want to write again the numbers i want to re use what already inside the block , how can I make it work ?

ITBYD
  • 83
  • 1
  • 9

1 Answers1

0

let birthday = {
  birthYear: 2010,
  birthDay: 3,
  birthMonth: 8,
  birthdayCheck: function() {
    var today = new Date();
    var currentDay = today.getDate();
    var currentMoth = today.getMonth() + 1
    var currentYear = today.getFullYear()
    if (currentMoth > this.birthDay || (this.birthDay <= currentDay && currentMoth >= this.birthMonth)) {
      this.myAge = currentYear - this.birthYear;
    } else {
      this.myAge = (currentYear - this.birthYear) - 1;
    }
  }
};

birthday.birthdayCheck();
console.log(birthday.myAge);

var today = new Date();
var currentDay = today.getDate();
var currentMoth = today.getMonth() + 1
var currentYear = today.getFullYear()


function birthdayCheck(birth_day, birth_month, birth_year) {
  if (currentMoth > birth_day || (birth_day <= currentDay && currentMoth >= birth_month)) {
    return (currentYear - birth_year)
  } else {
    return (currentYear - birth_year) - 1
  }
}

let birthday = {
  birthYear: 2010,
  birthDay: 3,
  birthMonth: 8,
}

birthday.myAge = birthdayCheck(birthday.birthDay, birthday.birthMonth, birthday.birthYear);
console.log(birthday.myAge);
birthday.myAge = birthdayCheck.apply(null, Object.values(birthday).reverse());
console.log(birthday.myAge);
Chandan
  • 11,465
  • 1
  • 6
  • 25
  • I dont want to use function inside the block , i want to create function outside and use it inside the object – ITBYD Feb 28 '22 at 17:32
  • @AngusYoungus you cannot use `this` like you are using in your example because this is not bound to `birthday` object scope birthday object is still undefined as per javascript and this is bound to outer scope which does not have the variable, if the you are not going to change the function then either you can pass the value specifically by using the `birthday` variable or you can use the `apply` method, I have updated the answer for reference – Chandan Mar 01 '22 at 17:55