A
A
Andrey Titov2020-03-01 18:20:58
JavaScript
Andrey Titov, 2020-03-01 18:20:58

How to find out what an incoming parameter is in a function, in someone else's code?

In the yearsDiff(dt)
@dt function - what is that value?

// @passportDate - дата выдачи паспорта
// @dudeDate - дата рождения чувака
function checkPassportDate(passportDate, dudeDate)
{
    var dob = new Date(dudeDate.replace(/(\d{2}).(\d{2}).(\d{4})/, "$3-$2-$1"));
    var pssprtDate = new Date(passportDate.replace(/(\d{2}).(\d{2}).(\d{4})/, "$3-$2-$1"));

    var pDate20 = new Date(dob);
    pDate20.setFullYear(pDate20.getFullYear() + 20);
    var pDate45 = new Date(dob);
    pDate45.setFullYear(pDate45.getFullYear() + 45);

    var ageDude = parseInt(yearsDiff(new Date(dudeDate.replace(/(\d{2}).(\d{2}).(\d{4})/, "$3-$2-$1"))));

    // первая смена паспорта
    if (ageDude >= 20 && ageDude < 45) {
        if (pssprtDate < pDate20) { return false; }
    }

    // вторая смена паспорта
    if (ageDude >= 45) {
        if (pssprtDate < pDate45) { return false; }
    }

    return true;
}

// разница в годах между текущей датой и @dt
function yearsDiff(dt)
{
    if (dt > new Date()) { return 0; }

    var crntDate = new Date();

    var yearDiff = parseInt(crntDate.getFullYear() - dt.getFullYear());
    
    // прошёл уже текущий год или ещё нет
    var dat4check = new Date(dt);
    dat4check.setFullYear(crntDate.getFullYear());
    if (dat4check > crntDate) { yearDiff--; }

    if (yearDiff <= 0) { return 0; }

    if (yearDiff === 1)
    {
        var monthDiff = parseInt(crntDate.getMonth() - dt.getMonth());
        if (monthDiff >= 0)
        {
            if (monthDiff == 0) {
                var dayDiff = parseInt(crntDate.getDate() - dt.getDate());
                if (dayDiff > 0) { return yearDiff; }
                else { return 0; }
            }
            else {
                return crntDate.getFullYear() - dt.getFullYear();
            }
        }
        else { return 0; }
    }
    else { return yearDiff; }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-03-01
@titov_andrei

Here dt is an object of type Date, considering how it is used in the code.
In general, see how the function you are interested in is called.

A
Arseny, 2020-03-01
Matytsyn @ArsenyMatytsyn

1. Read the code, understand it.
2. Understand that the author was too lazy to come up with a name for people.
3. Accept it, come to terms with it and move on with your life.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question