JavaScript 4
Exercise 1
<!doctype html>//1. Enter a character from the frame varc = Prompt ("Please enter a character:"); //2, judge whether the number varIsnum = C >= ' 0 ' && C <= ' 9 '; //3. Judge whether it is in English //3.1 To determine whether it is lowercase English varIssmall = C >= ' A ' && c <= ' z '; //3.2 To determine whether to capitalize English varIscaps = C >= ' A ' && C <= ' Z '; //3.3 Whether to save in English with a variable varIseng = Issmall | |Iscaps; //4. Judging whether it is a Chinese character varIschn = C >= ' \u4e00 ' && C <= ' \u9fa5 '; //5, according to the results of the printed output if(isnum) {Console.log (c+ "is a number"); }Else if(Iseng) {Console.log (c+ "is an English character"); }Else if(ISCHN) {Console.log (c+ "is a Chinese character"); }Else{Console.log (c+ "is other characters"); } </script> </body>View Code<!doctype html>varInput = prompt ("Welcome to China Mobile \N1, check the charges please press 1\N2, to understand the broadband please press 2\N3, for Chinese presses 3\N4, manual services please press 0"); Switch(input) { Case"1": Alert ("In the phone call, please later ..."); Break; Case"2": Alert ("Broadband service not yet open ..."); Break; Case"3": Alert ("Sorry, I don't know English ..."); Break; Case"0": Alert ("Manual agent Busy, please later ..."); Break; default: Alert ("Input Error!!!"); } </script> </body>View Code<!doctype html>varInput = prompt ("Please enter a number (0-7):"); Switch(input) { Case"1": Console.log ("Eat pork Today"); Break; Case"2": Console.log ("Eat braised fish today."); Break; Case"3": Console.log ("Eat the braised king eight today."); Break; Case"6": Case"7": Console.log ("Rest today."); Break; default: Console.log ("Input Error!!!"); Break; } </script> </body>View Code<!doctype html>varYear = number (prompt, ' year: ')); varmonth = number (' Month: ' Prompt)); varDay = number (Prompt (' Day: '))); varTotaldays = 0; varisrun=year%4==0&&year%100!=0| | Year%400==0; Switch(month-1){ Case11: Totaldays+ = 30; Case10: Totaldays+ = 31; Case9: Totaldays+ = 30; Case8: Totaldays+ = 31; Case7: Totaldays+ = 31; Case6: Totaldays+ = 30; Case5: Totaldays+ = 31; Case4: Totaldays+ = 30; Case3: Totaldays+ = 31; Case2: Totaldays+ = 28; if(isrun) {totaldays+ = 1; } Case1: Totaldays+ = 31; } totaldays+=Day ; Console.log ("+totaldays+" is "the first day"); </script> </body>View Code<!doctype html>//1, declaring the loop condition, assigning a value of 1 var i = 1; // 2. Cyclic structure: The condition when I is less than or equal to 100, perform the operation while (i <=) { console.log ("Hello World"); // Update loop condition i++; // i=i+1;i+=1; } </script> </body>
View Code<!doctype html>//condition: End from 1 to 100 //Action: Print the value of the loop condition vari = 1; while(I <= 100) {console.log (i); //Update loop conditioni++; } </script> <!--<script>//conditions: Starting from 1, to 100 end //action: The value of the cumulative loop condition varI=1,sum=0; while(I <= 100) {sum+ = i;//sum = sum + i;i + +; } console.log ("1-100 between the and for:" +sum); </script> <!--<script>//condition: End from 1 to 100 //action: Determine if the loop condition is a multiple of 3 varI=1; while(I <= 100){ if(i% 3 = = 0) {console.log (i); } I++; } </script> <!--<script>//condition: End from 2000 to 2050 //action: Determine whether the condition meets the requirements of a leap year vari = 2000; while(I <= 2050){ if(i%4==0&&i%100!=0| | I%400==0) {console.log (i); } I++; } </script> <script>varYear = number (prompt, ' year: ')); varmonth = number (' Month: ' Prompt)); varDay = number (Prompt (' Day: '))); varTotaldays = 0; varisrun=year%4==0&&year%100!=0| | Year%400==0; //condition: Starting from January, until month-1 month //action: Accumulate the number of days of these months vari = 1; while(I <month) { Switch(i) { Case1: Case3: Case5: Case7: Case8: Case10: Totaldays+ = 31; Break; Case4: Case6: Case9: Case11: Totaldays+ = 30; Break; Case2: Totaldays+ = 28; if(isrun) {totaldays+ = 1; }} I++; } totaldays+=Day ; Console.log ("Total" +totaldays+ "Days"); </script> </body>View CodeBranching structure
1,IF structure (slightly)
2,switch structure
function: equivalent judgment;
Syntax: switch (variable) {
Case value 1:
Statement Block 1;
break;//jump out of the switch structure, optional
Case value 1:
Statement Block 1;
break;//jump out of the switch structure, optional
。。。。。。。
Default
statement block N;
/* When all case is not matched, execute default*/
}
Note: 1, variables and case values after the judgment, the use of = = = to determine;
2,break, if break is omitted, start with a matching case block, execute down (execute all the contents of the case block below or the contents of default) until the break is encountered or the execution ends;
Practice:
Enter 1-7 any number from the box to indicate that the week 1~ Sunday
Input 1: Eat pork today
Input 2: Eat braised fish today
Input 3: Eat braised King eight today
Input 4: Eat braised leather shrimp today
Input 5: Eat braised ribs today
Input 6: Rest today
Input 7: Rest today
Other: Wrong input!
Loop structure
Function: Repeated execution of the same or similar code;
Two elements of the loop:
(1) Cycle conditions: When the cycle starts, when to end;
(2) Loop operation: What to do in the loop (the code to execute);
Ex
1. Console output 100 times Hello World
Condition: from 1th to 100th times
Action: Output Hello World
2, print all the numbers between 1-100
Condition: from 1th to 100th times
Action: The value of the output condition
While loop
Syntax: while (loop condition) {
Looping operations
}
Process:
1. Judging the cyclic condition (Boolean/Expression)
2. If the condition is true, the loop operation is performed
2.1 After performing the operation, then come back to judge the condition ...
3. If the condition is false, exit the loop
Ex
1. Print 100 times Hello World
Conditions: Starting from 1, to 100 end
Action: Print Hello World
Practice:
1, print all the numbers between 1-100
2. Calculate and print output of all numbers between 1-100
3. Print a number of multiples of 3 between 1-100
4. Print all leap years between 2000-2050
5. Change the date calculator to a circular version
Article Webbase: nineth, JavaScript knowledge 4