JavaScript program case

Source: Internet
Author: User
Tags true true

1. Ask for positive and

Requirements: Enter 10 numbers, find the positive number and positive numbers, and enter 999 to close the program:

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" >
var sum=0
var p=0
for (Var i=1;i<=10;i++) {
var num=parseint (Prompt ("Please enter the" +i+ "number (enter 999 to end the program))
if (num==999) break;
else if (num<0) continue;
else sum=sum+num,p=p+1;
}
document.write ("The program terminates with a result of" +sum+ "number is" +p ")
</script>
<body>
</body>

2. Date of request

Requirement: Enter year, month, date to find out the day of the year

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<script type= "Text/javascript" >

var year=parseint (Prompt ("Please enter the Year")
var num1=year%4;
var num2=year%100;
var num3=year%400;
var s=28
num1==0&&num2!=0| | num3==0?s=29:s=28;
var sum=0
var month=parseint (Prompt ("Please enter month"))

if (month==1| | month==3| | month==5| | month==7| | month==8| |month==10| | MONTH==12)
{var day=parseint (prompt ("Please enter date (1-31)")
if (day>=1&&day<=31) day=day
else document.write ("date input error")
}
else if (month==4| | month==6| | month==9| | MONTH==11)
{var day=parseint (prompt ("Please enter date (1-30)")
if (day>=1&&day<=30) day=day
else document.write ("date input error")
}
else if (month==2)
{var day=parseint (prompt ("Please enter date (1-" +s+ "))
if (day>=1&&day<=s) day=day
else document.write ("date input error")
}
else document.write ("Month input error")
for (Var i=1;i<month;i++) {
if (i==1| | i==3| | i==5| | i==7| | i==8| |i==10| | I==12) {
sum+=31;
}else if (i==4| | i==6| | i==9| | i==11) {
sum+=30;
}else if (i==2) {
Sum+=s;
}
}
document.write (year+ "year" +month+ "month" +day+ "Day is the first" + (Sum+day) + "Day" of the year)
</script>
<body>
</body>

3. Common JavaScript logic statements

"If-else Structure"
1, the structure of the wording:
if (judging condition) {
Execute when condition is true
}else{
Execute if condition is false
}
2, if () the expression, the result of the operation should be:
①boolean:true true False False
②string: Non-empty string for vacuum string is False
③number:0 is false everything is not 0, are true
④null/nan/undefined: All is False
⑤object: All is True
3, else{} structure, can be omitted according to the specific circumstances;

var num = {};

if (num) {
Execute when condition is true
document.write (if condition is established);
}else{
Execute if condition is false
document.write ("If condition is not established");
}
Num<10?document.write ("If condition"):d ocument.write ("If condition is not established");



"Multiple If, ladder if"
1, the structure of the wording:
if (condition i) {
Condition One established
} else if (condition two) {
Condition one not established && condition II established

Else-if section, can have more than n
} else{
Condition one not established && condition two not established
}

2, multiple if structure, each judgment condition is mutually exclusive! Only one of the roads can be selected.

3, If/else {} can be omitted, but generally not advocated;
If you omit {}, the IF/ELSE structure contains code that is only the most recent line (the semicolon ends);
If you omit {}, the else structure always belongs to the nearest if structure in front of it.


var num2 = 5;
if (num2>5) {
document.write ("Input too large");
} else if (num2<5) {
document.write ("Input too small");
} else if (num2==5) {
document.write ("input correct");
}

if (num2==5) document.write ("Input too large");

if (num2==5) document.write ("Input too large");
else document.write ("11");

document.write ("22");


"Nested IF Structure"
1, the structure of the wording:
if (condition i) {
Condition One established
if (condition two) {
Condition I set up && Condition II also established
}else{
Condition One establishment && condition two not established
}
}else{
Condition one is not tenable
}

2, if structure can be multi-nesting, but in principle, not more than 3 layers






"Switch-case Structure"
1, the structure of the wording:
switch (expression) {
case constant Expression 1:
Statement 1;
Break
Case constant Expression 2:
Statement 2;
Break
......
Default
Statement n
Break
}
2. Precautions:
The expression in ①switch (), and the expression following each case, can be any of the data types supported by JS (objects and arrays do not work);
All constant expressions following the ②case must be different, otherwise only the first one will be executed;
The constants after ③case can be any data type; different case of the same switch structure can be many different data types;
④switch in the judgment of the time, the use of a congruent judgment = = =.
⑤break function: After executing the case code, jump out of the current switch structure;
The consequences of missing a break: Start with the correct case and execute all case and default in turn. Reason: ⑥↓
⑥switch structure When judging, will only judge the correct answer, when the correct case is encountered, will not judge the subsequent project. Execute in turn.
⑦switch structures are executed faster than multiple if structures. In a multi-channel branch, you can prioritize the use of the switch structure.

var num4 = 5;
Switch (NUM4) {
Case 4:
document.write ("This is a case block of 9!") ");
Break
Case 5:
document.write ("This is a case block of 10!") ");
Break
Case 6:
document.write ("This is a case block of 11!") ");
Break
Default
document.write ("This is the case block of default!") ");
Break
}

JavaScript program case

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.