JS scripting language Base and array

Source: Internet
Author: User
Tags logical operators

JS and php, string assignment: to use "double quotation mark" or "single quotation mark" to cause: for example: var c= "hello"
Different types of mathematical operations, to convert, type conversions: cast parseint{c}---converted to integers, Parsefloat{}---convert decimals
An operator expression:
Mathematical operators: add, subtract, multiply, divide, Withdraw
Logical operators: and (&&): two conditions must be set up at the same time; : Two conditions one of the conditions satisfies; non (!): It turns out that the truth plus the non is the false Value.
Comparison operator: equals (= =) constant equals (= = =) Not equal to (= = or <>) example A: not equal to b:a!=b; greater than, less than, greater than or equal to, less than or equal to
Concatenation of strings: the symbol is "+" (mathematically added, string is two stitching Together)

For example:

      var a=10;      var b=20;      var s= (a+b);      Alert (s);

The return is: 1020
Ternary operators: simple criteria for judging
a==b? "equals": "not Equal to";
If the condition satisfies is return "equals", does not satisfy is "does not equal"

There are 3 types of statements:
Order (execution from top to bottom, plus ";" for statement End)
Branch (run to somewhere, if the condition is met, if it does not meet the criteria, another does not meet the criteria):
Condition Meets---if (a==b)
{the code executing the condition is satisfied}
The condition does not meet the----else{code that does not meet the conditions of execution}
Several common forms of if:
1. Simple if: only satisfies, for example: if (a==b) {alert (a+b);}
2.if+else:if (a==b) {alert (a+b);} else{alert (a*b);}
3.if+else if+else:if (a>10) {alert ("too expensive"),} else if (a>5) {alert ("ok"),} else (none of the above two conditions are satisfied) {alert ("too cheap");}
4.if Nesting: want to satisfy this if and want to satisfy another if
A.If () {
If () {alert ("");}
}
B.if (&&) {alert ("");}
1, for Loop (after qualifying, You can repeat a statement multiple times): for loop: initial value, condition judgment, State change, loop body
For (var I=0;i<10;i=i+1)
{
Alert (aa);
Break
}
a++ (first Action on a, then add one) \++a (first plus repeated operation a) equals a = A+1
Keyword break; jump out of the loop
continue; jump out of the current loop and continue next time
2, while Loop (dead loop) first judge the condition, then execute the loop body

var a=10;while (a>0) {    if (a=5)    {break;        }}
Or
var a=0
While (true)
{
a++;
}

3, do...while cycle (first executed loop body, in judging Conditions)
do{
Alert ("aa");
}
While (true)


Data Store---array
(1) strongly typed languages
A. Collection of data stores of the same type (contiguous in Memory)
B. Specifying a length when defining an array
foreach Notation:
foreach (int A in Attr)
{
}
A takes a value

(2) Weakly typed language
A. can store any type of data
B. non-contiguous in memory
JS in the wording of the Foreach:
var attr = [1,2,3,4,5];
For (var A in Attr)
{
Alert (a);//index of each element
}

var attr = new Array ();: Create an empty array and give it to the variable
var = new Array (attr1);: give default values when defined
var attr2 = ["aa", "bb", "cc"];: Create an array that also supports this form

Value and assignment of array data:
Assignment: attr[0]=10; There is an index of 0 with a value of 10
Value: Alert (attr1[0]),-----result is the extraction of "1";
Alert (attr1[1]);-----result is the extraction of "2";

Iterate through an array: all the contents of the array are found (traversed)
var attr = [1,2,3,4,5,6];
For (var I=0;i<6;i++)
{
Alert (attr[i]);
}
If you don't know how long an array is, you can use the length of the array to traverse
Length of array (number of elements): attr.length
For (var I=0;i<aeer.length;i++)
{
Alert (attr[i]);
}
70,89,78,90,96,79,88,100,96,95 sum, Maximum value, average
Sum:
var attr = [70,89,78,90,96,79,88,100,96,95];
var sum=0;
For (var I=0;i<attr.length;i++)
{
sum=sum+attr[i];
}
Alert (sum);
Maximum Value:
var attr = [70,89,78,90,96,79,88,100,96,95];
var maxn=0;
For (var I=0;i<attr.length;i++)
{
If (attr[i]>maxn)
{
maxn=attr[i]
}
}
Alert (maxn);
Average:
var attr = [70,89,78,90,96,79,88,100,96,95];
var sum=0;
For (var I=0;i<attr.length;i++)
{
sum=sum+attr[i];
}
Alert (sum);
var pingjunzhi=sum/attr.length;
Alert (pingjunzhi);

Array this piece is still possible, is the loop this piece, if the teacher said also understand, if do not speak directly to do mathematics problem, for mathematics problem is not, mathematics is my natural enemy ah, but still want to practice more, this is just can more, will be skilled.

  

JS scripting language base and array

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.