JavaScript built-in objects, Date String array, and so on, as well as these object operations.

Source: Internet
Author: User
Tags first string ming ord

Exercises:

A class of results come out, now the teacher to the class of results printed out.
:
May 9, 2014 Saturday-class is divided into: The average class is divided into:
Format requirements:
1. Display the date of printing. The format is similar to the current time of Wednesday, March 21, 2014.
2. Calculate the average (reserved integer) of the class.
student performance data are as follows:
"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: "
Task
The first step: You can get the current date by using JavaScript's Date object.
Tip: Use Date () Dates object, note that the week return value is 0-6, so you want to turn the word "Week 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.

Code implementation:

<1>html part

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<title>date.string</title>
<link rel= "stylesheet" type= "Text/css" href= "Styles/001.css"/>

<body>
<script src= "Styles/001.js" ></script>
<div id= "Time" ><input type= "button" value= "click to get" onclick= "Stime ()" /></p></div>

<div id= "Scor" >

<input type= "button" value= "click Get Average Score" onclick= "scppp ()"/>
<input type= "text" id= "Texttt"/>
</div>


</body>

<2>css part

*{margin:0;padding:0;}
body{
width:100%;

}
#time {
Margin:auto;
height:50px;

}
#scor {

width:100%;
height:60px;
margin-top:20px;
}

<3>js part

1. First method
//get time
function stime ()
{
var weekday=["Sunday", "Monday", " Tuesday "," Wednesday "," Thursday "," Friday "," Saturday "];

var d = new Date (); //return current date
var x=d.getfullyear (); //Return four-digit years
var y=d.getmonth (); //return current month 0: January
z=y+1;
S=d.getdate (); //Return day
var i=d.getday (); //Return to week 0: Sunday
document.getElementById ("Time"). innerhtml=x+ "Year" +z+ "month" +s+ "Day" +weekday[i];
}

//Student Performance
function Scppp () {
var scorestr = "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:";
var sco1=new Array ();
var j=0;
sco1=scorestr.split (":"); //with ":" to separate this and string, the segmented array elements are deposited into the SCO1 array
For (i=1;i<=sco1.length-1;i++) //due to Sco1 "0" There is no number so starting from 1 counts because the intention is to exclude SCO "0" (xiaoming) without numbers, but this is not quite in line with the programmed counting habit, Then we can use the IF statement to judge, for (i=1;i<=sco1.length-1;i++) if (parseint (sco "I" =! NaN) to add and
{

S=parseint (Sco1[i]); //Use parseint to extract numbers from array elements
J=j+s;

}
document.getElementById ("Texttt"). Value=parseint (j/(sco1.length-1));

}

2. The second method

var scorestr = "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: 76";

var Myarr = Scorestr.split (";"); / /First string split

var num = 0; var str;

for (Var i=0;i<myarr.length;i++) {

Str=myarr[i]; //Remove all elements of the array, each element is actually a string one-dimensional array

Myarr[i] = new Array (); ///Then the string of each element is then split

Myarr[i] = Str.split (":"); //Split mode, second string split, form two-dimensional array

Num =num + parseint (myarr[i][1]); //Two-D array }

document.write (num); //Extract the scores from the array, then sum them up, and output.

JavaScript built-in objects, Date String array, and so on, as well as these object operations.

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.