迅雷前端的两道编程题题目
迅雷前端的两道题目还是可以做的!!!不过第二题我不知道为啥一直不能通过...我测试是可以完全通过的~跟大家交流一下~
//第一道 EventEmitter类class EventEmitter {on(event, fn) {this._callbacks = this._callbacks || {};(this._callbacks['$' + event] = this._callbacks['$' + event] || []).push(fn);return this;}once(event, fn) {function on() {this.remove(event, on);fn.apply(this, arguments);}on.fn = fn;this.on(event, on);return this;}remove(event, fn) {this._callbacks = this._callbacks || {};if (0 == arguments.length) {this._callbacks = {};return this;}var callbacks = this._callbacks['$' + event];if (!callbacks) return this;if (1 == arguments.length) {delete this._callbacks['$' + event];return this;}var cb;for (var i = 0; i < callbacks.length; i++) {cb = callbacks[i];if (cb === fn || cb.fn === fn) {callbacks.splice(i, 1);break;}}return this;}emit(event) {this._callbacks = this._callbacks || {};var args = [].slice.call(arguments, 1),callbacks = this._callbacks['$' + event];if (callbacks) {callbacks = callbacks.slice(0);for (var i = 0, len = callbacks.length; i < len; ++i) {callbacks[i].apply(this, args);}}return this;}}//第二道 学生排序function sortStudents(obj1, obj2) {if (obj1["class"] === obj2["class"]) {var val1 = obj1["score"];var val2 = obj2["score"];if (val1 < val2) {return 1;} else if (val1 > val2) {return -1;} else {return 0;}} else if(obj1['class'] === obj2['class'] && obj1["score"] === obj2["score"]){return 0;} else {var val1 = obj1["class"];var val2 = obj2["class"];if (val1 < val2) {return -1;} else if (val1 > val2) {return 1;} else {return 0;}}}var arr = [{ "name": "张三", "class": 2, "score": 64 }, { "name": "李四", "class": 1, "score": 80 }, { "name": "王五", "class": 1, "score": 80 }, { "name": "赵六", "class": 4, "score": 94 }];console.log(arr.sort(sortStudents));
#前端##迅雷##笔试题目##题解#