迅雷的两题,可以,很简单
第一题:
/*请完成下面这个函数,实现题目要求的功能 ******************************开始写代码******************************/ function EventEmitter() { var onEvents = {}, onceEvents = {}; this.on = function(event, listener){ if (event in onEvents) { onEvents[event].push(listener); } else { onEvents[event] = [listener]; } }; this.once = function(event, listener){ if (event in onceEvents) { onceEvents[event].push(listener); } else { onceEvents[event] = [listener]; } }; this.emit = function(event, ...args){ var onList = onEvents[event], onceList = onceEvents[event]; if (onList) for (var i in onList) onList[i].apply(null, args); if (onceList) { for (var i in onceList) onceList[i].apply(null, args); delete onceEvents[event]; //onceEvents[event].length = 0;//清空once的*** } }; this.remove = function(event, listener){ var onList = onEvents[event], onceList = onceEvents[event]; if (onList) while(onList.indexOf(listener) !== -1) { onList.splice(onList.indexOf(listener),1); } if (onceList) while(onceList.indexOf(listener) !== -1) { onceList.splice(onceList.indexOf(listener),1); } }; } /******************************结束写代码******************************/ var count = 0 var input = read_line() eval(input) printsth(count)
第二题:
function sortStudents(students) {students.sort((a,b)=>{if (a.class === b.class)if (a.score === b.score)return 0;else return b.score - a.score;else return a.class - b.class;});return JSON.stringify(students);}