1. js statement<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test js statement</title> <!-- Embed js code in HTML --> <script> // 2. Loop structure // Exercise 3: Output the results in the console. Output how many days it will take to spend 100 million if half of it is spent every day. Use f12 to see var f = 0; //Record the number of days var g = 100000000; //Record the money while( g > 1){ g = g/2; //Change money f++; //Statistics day++ } console.log(f); //Exercise 1: Output the results in the console. Use f12 to output 1 to 10 for(var i = 1;i < 11;i++){ console.log(i); } //Exercise 2: Output the result in the console, output the sum of 1 to 10, see f12 var e = 0; //Record the sum for(var i = 1;i < 11;i++){ // e = e+ i; e += i; } console.log(e); // 1. Branch structure //Exercise 4: Pop up the day of the week based on the number entered by the user //var d = prompt("Please enter the day of the week:") ;//The default is string type var d = parseInt( prompt("Please enter the day of the week:") ) ;//string->number switch(d){ case '1' : console.log("Today is Sunday 1"); // Output the result in the console, use f12 to view case 2 : alert("Today is Sunday 2"); break; case 3 : alert("Today is Sunday 3"); break; case 4 : alert("Today is Thursday"); break; case 5 : alert("Today is Friday"); break; case 6 : alert("Today is Saturday"); break; case 7 : alert("Today is Sunday 7"); break; } //Exercise 3: Determine whether a common year is a leap year (divisible by 4 and not divisible by 100 | divisible by 400) var c = prompt("Please enter the year:"); if( c%4==0 && c%100!=0 || c%400==0 ){ alert("leap year"); }else{ alert("Normal Year"); } //Exercise 2: Receive the score entered by the user and determine the grade to which the score belongs var b = prompt("Please enter the score:"); if( b>=80 && b<=100 ) alert("Excellent"); else if( b>=60 && b<80 ) alert("Medium"); else if( b>=0 && b<60 ) alert("failed"); else alert("Input error"); //Exercise 1: If a>10, play 1, otherwise play 0 var a = prompt("Please enter an integer"); if( a > 10 ){ alert(1); }else{ alert(0); } </script> </head> <body> </body> </html> Second, js array<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test js array</title> <!-- Arrays in js, characteristics of arrays in Java: length cannot be changed, single data type--> <script> //1. js defines an array, features: variable length, rich data types var a = new Array(); //Empty array a = new Array(10,1.1,'hello',true,null,10,1.1); //Change the length of a console.log(a); //View the data in the array console.log(a.length); //Get the length of a a[99] = 0; console.log(a); //View the data in the array console.log(a.length); //Get the length of a, 100 var b = [ ] ; //empty array b = [1,2,3,"jack",1.1];//assignment for(var i = 0 ; i < b.length ;i++){//traversal console.log(b[i]);//print data according to subscript } //Exercise 1: Find the sum of even numbers in an array var c = [1,2,3,4,5,6,7,8]; //Equivalent to java's foreach,,,,for...in for(var i in c){ console.log(c[i]); } var d = 0 ; // record and for(var i=0 ;i < c.length ;i++){ if(c[i] % 2 == 0){ d+=c[i]; // sum } } console.log(d); </script> </head> <body> </body> </html> 3. js function<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Test js function</title> <script> // 1. Function writing method 1: a block of code with a specific function // Function declaration: function function name (parameter list) {function body} function a(){ var b=[1,2,2,3,4];//define array for(var i in b){//traverse array console.log(b[i]);//print data } } // Call function a(); // 2. Function writing 2: a block of code with a specific function //Declare function: var function name = function(parameter list){function body} var b = function(){ console.log(100); } //Call: function name (parameter list); -- The called function must exist! ! b(); //3. Define and call a function with parameters function c(a,b){ //Define a function with parameters console.log(a+b); } c("hello",1); //Calling a function with parameters var d = function(a,b){ console.log(a+b); } d(1.1,2.6); //4. Define and call a function with parameters and return value function e(a,b){ return a+b; //Return the result to the caller } var f = e(1,2); console.log(f); var g = function(){ return "hello js" ; //Return the result to the caller } var h = g(); console.log(h); //Exercise: Count the number of times character a appears in a string function cishu(str,chara){ var count = 0 ;// record the number of times for(var j = 0 ; j < str.length ;j++){//traverse the string var data = str.charAt(j);//get each character //compare with character a if(data == chara){ //If it is a character, then ++ count++; } } return count; //Return to the caller } //Call the cishu function to count the number of times character a appears in the string abcaaaaa var count = cishu("abcaaaaa",'a'); console.log("The number of times string a appears is: "+count); </script> </head> <body> </body> </html> 4. MavenV. ConclusionThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: How to configure Openbox for Linux desktop (recommended)
>>: Detailed explanation of MySQL index principles and optimization
Table of contents front end According to the abov...
My page today also had garbled characters, so I s...
<button> tag <br />Definition and usag...
Text shadow text-shadow property effects: 1. Lowe...
The following error occurs when entering Chinese ...
Table of contents Preface 1. What is a lock? 2. L...
Table of contents 1 Create mount directories and ...
The final result is like this, isn’t it cute… PS:...
Regarding some MySQL specifications, some compani...
After the form is submitted, the returned HTML pag...
Version update, the password field in the origina...
By default, Docker runs over a non-networked UNIX...
Table of contents 1. Install Docker 2. Create a c...
This article example shares the specific code of ...
Let’s take a look at the renderings first: XML/HT...