There is a task to obtain data about a person and calculate days to others with the help of classes and constructors using a het, a set.

class Persona { constructor(first, last, day, month, year) { this.name = { first, last }; this.Birthday = { day, month, year }; } get fullName() { return `${this.name.first} ${this.name.last}`; } } var person1 = new Persona('Name', 'Surname', 11, 11, 1111); console.log(person1.fullName); 

I didn’t finish the code, because I don’t understand how to calculate correctly, I found a function that, in theory, it does.

  function daysLeft() { var year = parseInt(prompt('enter year (format: YYYY)')); var month = parseInt(prompt('enter month (format: M)')); var day = parseInt(prompt('enter day')); var today = new Date(); today.setHours(0,0,0,0); var nextDate = new Date([today.getFullYear(),month,day].join(',')); if (nextDate < today) nextDate.setFullYear(today.getFullYear()+1); msPerDay = 24*60*60*1000; daysLeft = Math.round((nextDate.getTime() - today.getTime())/msPerDay); dayname = ""; ds = ""+daysLeft; dd=parseInt(ds.substr(ds.length-1)); } } 

help pliz correctly write everything down

1 answer 1

As I understand you need this:

 class Persona { constructor(first, last, day, month, year) { this.name = { first, last }; this.Birthday = { day, month, year }; } get fullName() { return `${this.name.first} ${this.name.last}`; } get daysToBirthday() { const today = new Date(); today.setHours(0, 0, 0, 0); const nextDate = new Date([today.getFullYear(), this.Birthday.month, this.Birthday.day].join(',')); if (nextDate < today) { nextDate.setFullYear(today.getFullYear() + 1); } const msPerDay = 24 * 60 * 60 * 1000; return Math.round((nextDate.getTime() - today.getTime()) / msPerDay) - 1; } } const person1 = new Persona('Name', 'Surname', 31, 12, 1990); console.log(person1.fullName); console.log(person1.daysToBirthday)