Experiment 1 JavaScript Basics

1. Experimental purposes and requirements

Experimental tasks

1. Master the syntax rules of JavaScript

2. Master JavaScript variable declarations

3. Familiar with JavaScript data types

4. Master the various operators of JavaScript

5. Master JavaScript function definitions

Training skill points

  1. Master variable definitions
  2. Master the application of if conditional statements
  3. Master the application of for loop
  4. Master data type conversion
  5. Master the definition of parameterized functions

2. Experimental instruments and equipment. Computers
equipped with Dreamweaver , HBuilder and other software, and a smooth network.

3. Experimental tasks and steps

Experiment 1. Output triangle

Statement of needs

Enter the number of rows to print triangles, and print triangles on the page. No matter whether the number of input triangle rows is greater than 8, the number of output triangle rows is at most 8. The triangles output on the page are as shown in the figure:

          

 

          Figure 1 Enter the number of lines to print Figure 2 Output triangles on the page

Implementation ideas and steps

  1. Use  the prompt() method to enter the number of rows of the triangle, and call the parseInt() method to convert the row number to an integer.
  2. Use   an if   statement to determine whether the number of rows is less than 8.

     3. Use  a double  for loop to output the triangle, use the outer loop variable to control the  rows  , and the inner loop variable to control the number of * numbers in each line  . The * number in each line is output and the line breaks .

Experiment 2. Writing functions

Statement of needs

Write the function f(x)=5x^2+9x-15, use the prompt dialog box to input the value of x, and output the corresponding result on the page.

Implementation ideas and steps

1. Define the function and express the function using addition, subtraction and multiplication operators.

2. Use  the prompt()  method to enter the value of x.

3. Call the defined function.

4. Use the document.write()  method to output the corresponding results on the page.

         

     Figure 1 Enter the value of x Figure 2 Output the result on the page

4. Experimental code demonstration

Experiment 1 code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>打印三角形</title>
		<style type="text/css">
			body{text-align: center;}
		</style>
	</head>
	<body>
		<script type="text/javascript">
			var row=parseInt(prompt("请输入三角形的行数:"));
			if(row<=8){
				for(var i=0;i<row+1;i++){
					for( var j=0;j<i;j++){
						document.write("*");
					}
					document.write("</br>");
				}
			}
			else{
				for( var i=0;i<9;i++){
					for(var j=0;j<i;j++){
						document.write("*");
					}
					document.write("</br>");
				}
			}
		</script>
	</body>
</html>

Experiment 2 code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>计算函数表达式</title>
		<script type="text/javascript">
			function f(x){
				var y=5*x*x+9*x-15;
				document.write("结果是"+y);
			}
		</script>
	</head>
	<body>
		<script type="text/javascript">
			var x=parseInt(prompt("请输入一个数"));
			f(x);
		</script>
	</body>
</html>

5. Experimental results

Experiment 1 results:
Enter a number less than 8:

 

Enter a number greater than 8:


Experiment 2 results:

 

 

Guess you like

Origin blog.csdn.net/pzcxl/article/details/130313087