Front-end practice--javascript--built-in objects

Source: Internet
Author: User

Relevant knowledge points:

    • JavaScript Learning note Seven;

    • Web Course--"JavaScript Advanced chapter"

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.