Relevant knowledge points:
Task : A class of results came out, now the teacher to the class of the results printed out.
:
XXXX年XX月X日星期X--班级总分为:81
Format requirements:
1. Display the date of printing. The format is similar to the current time of "xxxx year XX month XX day week X".
2. Calculate the average (reserved integer) of the class.
Student performance data are as follows:
"小明:87; 小花:81; 小红:97; 小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"
Task decomposition:
The first step: you can get the current date by using JavaScript's Date object.
提示:使用Date()日期对象,注意星期返回值为0-6,所以要转成文字"星期X"
The second step: a long-channeling string is not good, find the rule after the division into the array to better operate OH.
Step three: Divide the string to get the score, then sum the rounding.
提示:parseInt() 字符串类型转成整型。
Solution Code:
<! DOCTYPE html><html ><head><meta http-equiv="Content-type" Content="text/html; Charset=utf-8 " /><title>Fasten your seatbelt and get ready to sail.</title><script type="Text/javascript">varMydate=New Date();varweekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];varMynum=mydate.getday ();varMyyear=mydate.getfullyear ();varMymonth=mydate.getmonth ();varMyday=mydate.getdate ();d Ocument.write ("Current time:"+myyear+"Year"+mymonth+"Month"+myday+"Day"+"a"+weekday[mynum]+"<br>");//Through the date object of JavaScript to get the current date, and output. varMystr="Xiao Ming: 87; Floret: 81; Little Red: 97; Small day: 76; Xiao Zhang: 74; little: 94; Xiao XI: 90; Xiao Wu: 76; Dicky: 64; Small ord: ";varScorearr=New Array(); Scorearr=mystr.split (";");//score is a long-channeling string bad processing, find a regular post-segmentation into the array better operation Oh //Extract the scores from the array, then sum them up, and output. varScore =New Array();varsum =0; for(vari =0; i < scorearr.length; i++) {sum + =parseint(Scorearr[i].substr (Scorearr[i].indexof (":")+1)); } document.write ("--the class is divided into:"+ sum); document.write (", the class is divided evenly (reserved integers):"+Math. Round (Sum/scorearr.length));</script></head><body></body></html>
Demo Result:
Front-end practice--javascript--built-in objects