腾讯前端笔试

1,2,3,5题都很简单,就不多提了。
第四题写了一阵子,一开始通过50%,后来发现问题出现在2月份的天数上,一般是28天,闰年是29天。
判断闰年:(year%4==0&&year%100!=0)||year%1000==0
function Calendar(container, year, month) {
    console.log(container)
    this.year = year;
    this.month = month;
    this.html = html;
    this.el = document.createElement('table'); //TODO: 创建分页组件根节点
    // if (!el) return;
    this.el.className = 'calendar';
    this.el.innerHTML = this.html();
    container.appendChild(this.el);

    this.el.addEventListener('click', event => {
        var el = event.target;
        var isPre = el.classList.contains('pre');
        var isNext = el.classList.contains('next');
        if (!isPre && !isNext) return;
        if (isPre) {
            //TODO: 更新this.month和this.year
            if (this.month == 1) {
                this.year -= 1
                this.month = 12
            } else {
                this.month -= 1
            }
        } else {
            //TODO: 更新this.month和this.year
            if (this.month == 12) {
                this.year += 1
                this.month = 1
            } else {
                this.month += 1
            }
        }
        this.el.innerHTML = this.html();
    });

    function html() {
        var date = new Date();
        var year = +this.year || 0;
        var month = (+this.month || 0) - 1;
        var day = date.getDate();
        var current_month = date.getMonth();
        var current_year = date.getFullYear();
        //TODO: 生成组件的内部html字符串
        let head =
            `<thead><tr><th class="pre"><</th><th colspan="5" class="date">${year}.${month<9?'0'+(month+1):String((month+1))}</th><th class="next">></th></tr><tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr></thead>`
        let body = ''
        let start = new Date(year, month, 1).getUTCDay()
        let days = 31
        if (month == 3 || month == 5 || month == 8 || month == 10) {
            days = 30
        }
        if (month == 1) {
            if ((year % 4 == 0 && year % 100 != 0) || year % 1000 == 0) {
                days = 29
            } else {
                days = 28
            }
        }
        let count = 0
        for (let i = 0; i < 6; i++) {
            if (i == 5 && count - start + 1 > days) {
                continue
            }
            body += '<tr>'
            for (let j = 0; j < 7; j++) {
                if (count >= start && count - start + 1 <= days) {
                    if (count - start + 1 == day && month == current_month && year == current_year) {
                        body += `<td class="current">${count-start+1}</td>`
                    } else {
                        body += `<td>${count-start+1}</td>`
                    }
                } else {
                    body += '<td></td>'
                }
                count++
            }
            body += '</tr>'
        }
        return head + body;
    }
}


#笔试题目##腾讯#
全部评论
还记得具体题目吗,上次做了这次我就没进去做了,想看看都是什么题
1 回复
分享
发布于 2020-09-06 22:13
可以用new Date(2020, 2, 0).getDate()获取二月份的天数
点赞 回复
分享
发布于 2020-09-06 22:17
联易融
校招火热招聘中
官网直投
tql,我接别人代码根本写不来的
1 回复
分享
发布于 2020-09-06 22:26
第一题合并有序数组,只通过了60%,有知道原因的吗?其他都A了
点赞 回复
分享
发布于 2020-09-07 09:30
有多少人第四题能写出来的 这题我是基本放弃的 其他都a了
点赞 回复
分享
发布于 2020-09-08 07:44

相关推荐

2 10 评论
分享
牛客网
牛客企业服务