text “(2) - 清洁代码”由Szymon Zalarski撰写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text “(2) - 清洁代码”由Szymon Zalarski撰写相关的知识,希望对你有一定的参考价值。

let sessions = ['2017-03-10 08:13:11', '2017-03-10 19:01:27', '2017-03-11 07:35:55', '2017-03-11 16:15:11', '2017-03-12 08:01:41', '2019-03-10 17:19:08']
sessions = sessions.sort();
refactoredSession = [];

// the function checks whether the session occurs three days in a row
const checkLastThreeDays = (sessions) => {
    // sessions = sessions.sort();
    let allDaysInSession = sessions.map(item => item.day);
    //getting all days from sessions
    let uniqueDays = [...new Set(allDaysInSession)];
    //getting all unique days from sessions
    if(uniqueDays.length >= 3) {
        if(uniqueDays[uniqueDays.length - 1] - uniqueDays[uniqueDays.length - 2] == 1 && uniqueDays[uniqueDays.length - 2] - uniqueDays[uniqueDays.length - 3] == 1) {
            checkAmountOfSession(sessions, uniqueDays);
            //checking 3 days in row
        } else {
            console.log(false);
        }
    } else {
        console.log(false)
    }
}

const checkAmountOfSession = (sessions, uniqueDays) => {
    let sessionsCounter = 0;
    //sessionCounter counts the correct sessions
    let length = sessions.length - 1;
    //length is length of sessions array
    sessions.map((item, index) => {
        if(uniqueDays.includes(item.day)) {
            if(index == length) {
                return
            } else {
                if(item.year == sessions[index + 1].year) {
                    if(item.day == sessions[index + 1].day) {
                        //checks if day x is the same as day x + 1
                        if(item.hours == sessions[index + 1].hours || item.hours - sessions[index + 1].hours == -1) {
                            //checks if the break lasts longer than 30 minutes or is equal
                            let firstTime = 60 - item.minutes;
                            let secondTime = parseInt(sessions[index + 1].minutes);
                            let timeBreak = firstTime + secondTime;
                            if(timeBreak >= 30) {
                                sessionsCounter++
                            } else {
                                return
                            }
                        } else {
                            sessionsCounter++
                        }
                    } else {
                        sessionsCounter++
                    }
                } else {
                    return
                }
            }
        } else {
        }
    })
    if(sessionsCounter === length) {
        //if the number of correct sessions is equal to the number of sessions, the whole operation gives the true
        console.log('%c true', 'color: #52db25');
    } else {
        console.warn(false)
    }
}

//the function transforms sessions array to array of objects
const sessionsTransform = (sessions) => {
    let dayObj = {};
    sessions.map(item => {
        dayObj = {
            year: item.slice(0, 4),
            month: item.slice(5, 7),
            day: item.slice(8, 10),
            hours: item.slice(11, 13),
            minutes: item.slice(14, 16),
            seconds: item.slice(17, 19),
        }
        refactoredSession.push(dayObj)
    })
    checkLastThreeDays(refactoredSession);
}

sessionsTransform(sessions);

以上是关于text “(2) - 清洁代码”由Szymon Zalarski撰写的主要内容,如果未能解决你的问题,请参考以下文章

text 图像由Node.js任务缩小。

text 由SnippLeaf.com创建

text 由Native JS触发输入

text 很棒的博客由其他人发布

text 由action标签提交的mvc表单

text 由div类选择项目的默认选项