关于JS的一些问题
写一个var lectures = [
{ code: 'comp211', dow: 2, start: 1000, end: 1130, room: 'A203' },
{ code: 'comp211', dow: 5, start: 1000, end: 1130, room: 'A318' },
{ code: 'comp212', dow: 2, start: 1430, end: 1600, room: 'A318' },
{ code: 'comp212', dow: 2, start: 1600, end: 1730, room: 'A205' },
{ code: 'comp213', dow: 2, start: 1130, end: 1300, room: 'A318' },
{ code: 'comp213', dow: 4, start: 1000, end: 1130, room: 'A318' },
{ code: 'comp214', dow: 1, start: 1000, end: 1130, room: 'A214' },
{ code: 'comp214', dow: 1, start: 1130, end: 1300, room: 'A214' },
{ code: 'math211', dow: 4, start: 1130, end: 1300, room: 'A317' },
{ code: 'math211', dow: 5, start: 1130, end: 1300, room: 'A317' },
{ code: 'meng211', dow: 1, start: 1430, end: 1630, room: 'A301' },
{ code: 'meng211', dow: 4, start: 1430, end: 1630, room: 'A301' },
{ code: 'comp311', dow: 4, start: 1000, end: 1130, room: 'A214' },
{ code: 'comp311', dow: 4, start: 1130, end: 1300, room: 'A214' },
{ code: 'comp312', dow: 4, start: 1430, end: 1600, room: 'A207' },
{ code: 'comp312', dow: 5, start: 1130, end: 1300, room: 'A206' },
{ code: 'comp313', dow: 2, start: 1130, end: 1300, room: 'A321' },
{ code: 'comp313', dow: 5, start: 1000, end: 1130, room: 'A317' },
{ code: 'comp314', dow: 1, start: 1000, end: 1130, room: 'A318' },
{ code: 'comp314', dow: 1, start: 1130, end: 1300, room: 'A318' },
{ code: 'comp315', dow: 1, start: 1430, end: 1600, room: 'A206' },
{ code: 'comp315', dow: 1, start: 1600, end: 1730, room: 'A206' },
{ code: 'meng311', dow: 2, start: 1430, end: 1630, room: 'A311' },
{ code: 'meng311', dow: 5, start: 1430, end: 1630, room: 'M315' }];
function dayOfWeek(dow) {
for(var i=0; i<lectures.length;i++){
if(lectures[i].dow = 1){
return "Mon"}
if(lectures[i].dow = 2){
return "Tue"}
if(lectures[i].dow = 4){
return "Thu"}
if(lectures[i].dow = 5){
return "Fri"}
}}
关于code的排序(有字母和数字)
例如显示出来 就是像上面的排序 function ByCode(year){ returns a new array
given year, sorted by course code.}
另外一个市在写个时间的排序 function ByTime(year){ returns a new array
given year, sorted by date-of-week and start time of the lectures.}
JS?HTML?
[解决办法]
if(**==1)
=是赋值,==才是判断,先这样改过来再测试
[解决办法]
看不太懂你的问题。
如果是对lectures排序,那写两个排序函数就可以了:
function byCode() {
var resultArr = lectures.sort(codeSorter);
print(resultArr);
}
function byTime() {
var resultArr = lectures.sort(timeSorter);
print(resultArr);
}
function codeSorter(lectureA, lectureB) {
return lectureA.code - lectureB.code;
if ( lectureA.code< lectureB.code)
return -1;
if ( lectureA.code> lectureB.code)
return 1;
return 0;
}
function timeSorter(lectureA, lectureB) {
var dowResult = lectureA.dow - lectureB.dow;
if (dowResult != 0) {
return dowResult;
}
return lectureA.start - lectureB.start;
}
function print(arr) {
var result = new Array();
for (var i = 0; i < arr.length; i++) {
result.push("code:" + arr[i].code + ",dow:" + arr[i].dow + ",start:" + arr[i].start + ",end:" + arr[i].end + ",room:" + arr[i].room);
}
alert(result.join("\r\n"));
}