<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系好安全带,准备启航</title> <script type="text/javascript"> //通过javascript的日期对象来得到当前的日期,并输出。 var Time=new Date(); var weekdy=["日","一","二","三","四","五","六"], year=Time.getFullYear(), month=Time.getMonth()+1, date=Time.getDate(), day=Time.getDay(); document.write( "今天是:"+year+"年"+month+"月"+date+"日 星期"+weekdy[day]+"<br>"); //成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦 var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; //字符串转成数组,去掉连接符,反复3次 var news=scoreStr.split(":").join().split(";").join().split(","); var sorce=0; for(var i=1;i<news.length;i+=2){ sorce+=parseInt(news[i]); } document.write("总分"+sorce); var average=Math.round(sorce/(news.length/2)); document.write("平均分"+average); //从数组中将成绩撮出来,然后求和整,并输出。 </script> </head> <body> </body> </html>
month=Time.getMonth()+1,
为什么要+1啊求解答
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系好安全带,准备启航</title> <script type="text/javascript"> //通过javascript的日期对象来得到当前的日期,并输出。 var nowDate = new Date(); var year = nowDate.getFullYear(); var month = nowDate.getMonth()+1; var day = nowDate.getDate(); var weekday = ["星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]; var num = nowDate.getDay(); document.write(year+"年"+month+"月"+day+"日 "+weekday[num]+"--班级总分为:"); //成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦 var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; var sum = 0; var flag = 0; //记录循环次数即有多少个分数 for (var i=3;i<scoreStr.length;i+=6) { //从数组中将成绩撮出来,然后求和取整,并输出。 sum = sum + parseInt(scoreStr.substr(i,2)); flag = flag+1; } document.write(sum/flag); </script> </head> <body> </body> </html>