Definition of functions, syntax, two-dimensional arrays, several exercises

Source: Internet
Author: User

1, please put ' A ', ' B ', ' C ' in the array, and then output
2, please put "I" "Love" "You" into the array, and then forward and reverse the output
3, input 10 integers into the array, and then copy to the B array output
4. Define a 10-length array and loop through 10 integers. It then enters an integer, finds the integer, finds the output subscript, and does not find the hint.
5. A set of numbers is divided into two array odd even


1, please put ' A ', ' B ', ' C ' in the array, and then output
function One () {
var arr = ["A", "B", "C"];
for (var i = 0; i< arr.length; i++) {
document.write (Arr[i]);//=>arr[0] arr[1] arr[2]
}
}

2, please put "I" "Love" "You" into the array, and then forward and reverse the output

The first of these methods
var flag = true;
function () {
var arr = ["I", "Love", "You"];
var str = "";

if (flag) {
Positive output
for (var i = 0; i< arr.length; i++) {
str = str + arr[i]; //
Modified value document.write (Arr[i]);//=>arr[0] arr[1] arr[2]
}

document.getElementById (' AA '). value = str;
Flag = false;
}else{
Reverse output
arr = Arr.reverse ();
for (var i = 0; i< arr.length; i++) {
str = str + arr[i];
}
document.getElementById (' AA '). value = str;
Flag = true;//Modified value
}
}

2nd method

function Two_o () {
var arr = ["I", "Love", "You"];
for (var i = 0; i< arr.length; i++) {
document.write (Arr[i]);//=>arr[0] arr[1] arr[2]
}
}

function two_t () {
var arr = ["I", "Love", "You"];

for (var i = arr.length-1; i>=0; i--) {
document.write (Arr[i]);//=>arr[0] arr[1] arr[2]
}
}

3, input 10 integers into the array, and then copy to the B array output
function three () {
var a = [1,2,3,4,5,6,7,8,9,10];
var b = [];
var c = [];
for (var i = 0; i<a.length; i++) {
document.write ("+i+", "Second cycle <br>");
B[i] = A[i];
document.write ("Array B:" +b+ "<br>");
C.push (A[i]);
document.write ("Array C:" +c+ "<br>");

}

}

4. Define a 10-length array and loop through 10 integers. It then enters an integer, finds the integer, finds the output subscript, and does not find the hint.

Function Four () {
var arr = [],
num = 0,
Flag = true;
Marker

for (var i = 0; i<10;i++) {
Arr.push (Prompt ("input" + (i+1) + "number"));
}
num = Prompt ("Re-enter a number");

for (var i = 0; i<arr.length; i++) {
if (arr[i] = = num) {
document.write (i);
Flag = false;
}
}

if (flag) {
Alert ("not Found");
}

}

The following contents are placed inside the body:
<input type= "button" value= "Connection array concat" onclick= "TC ()" >
<input type= "button" value= "Job a" onclick= "one ()" >
<input type= "button" value= "Job Two" onclick= "()" >
<input type= "button" value= "Job two _1" onclick= "Two_o ()" >
<input type= "button" value= "Job two _2" onclick= "two_t ()" >
<input type= "button" value= "Job three" onclick= "three ()" >
<input type= "button" value= "Job four" onclick= "four ()" >


<input type= "text" id= "AA" >

The two-dimensional array is the popular point is the Var arr= "" "" "" ";

Exercises

Receive three students three grades saved as a two-dimensional array calculates the total score of each student, the average score, the total score of the class, the average of each course grade
function Savescore () {
var info = [[], [], []];
var sum = 0;
var ke = [0,0,0];
for (var i = 0; i<info.length; i++) {
Info[i][0] = prompt ("Please enter student name");
Info[i][1] = prompt ("Please input language score");
Info[i][2] = prompt ("Please enter math score");
Info[i][3] = prompt ("Please input English score");
INFO[I][4] = +info[i][1] + +info[i][2] + +info[i][3];
INFO[I][5] = INFO[I][4]/3;

sum = sum + +info[i][4]; Class Total
Ke[0] = ke[0] + +info[i][1];//Chinese Total
KE[1] = ke[1] + +info[i][2];//Math Total
KE[2] = ke[2] + +info[i][3];//English Total

}
for (var i = 0; i<info.length;i++) {
document.write (info[i][0]+ "'s Language Achievement:" +info[i][1]+ "; Mathematics:" +info[i][2]+ "; English score:" +info[i][3]+ "; Total:" +info[i][4]+ "; Average score: "+info[i][5]+" <br> ")
}
document.write ("Class total:" +sum+ "<br>")
document.write ("Chinese average achievement:" +ke[0]/3+ "; average score of mathematics:" +ke[1]/3+ "; English average:" +KE[2]/3)

The examples are many, listen to very clear, but they knock up the code, or some difficulty, after a lot of practice ah, ingenious, practice much, also makes perfect. An example of an instant knock out, this is the direction to work hard.

Definition of functions, syntax, two-dimensional arrays, several exercises

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.