Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Introduction to JavaScript Introduction to JavaScript Section 1:Java Script Syntax Java Script is a programming language that consist of a series of statements. These statements are used to tell computer to complete specific task. All statement are separated by semicolon. Example 1.1 var a = 1; var b=8; Tip: Java Script will ignores multiple white space. var a = 1; and var b=8; will be regarded as same format, which means var b=8; can also be written as var b = 8;. var a = 1; and var b = 8; are statements. They are separated by ; , and each statement start from a new line. In example 1.1, it uses var . var is called the keyword. It means variables. The keyword var told computer to build a variable( will be discussed later in section 2). Section 2: Java variables and data types Sometimes you want to use a variable to store a value in JavaScript, so that you can use it later. Then you need to know how to use a variable. Example 2.1 var x = 3; var y = 6; var z = x + y ; Example 1.1 gives x a value 3 and gives y a values 6, then it assign z to be the sum of x and y.Therefor the value of z is 9. Example 2.2 var distance = 5670; var speed = 123; var time = distance/speed; Tip Notice that in example 1.2, the sign of division is represented as / , and multiplication is represented as *. The = means assign value 5670 to variable distance. You can not only give these variable a integer value, but also other information like characters and strings. Example 2.4 var firstName = "Andy"; var gender = "Male"; Tip: Java Script is case sensitive, Which means var first firstname and var firstName are two different variables. In example 2.3, variable name and gender holds a String variable (must be inside double quotation marks). And in example 2.1 and 2.2 are Number variables. Sting and numbers are both primitive type variable. There are 6 primitive type. The other 4 are: Boolean, Null, Undefined and Symbol. (We will only use number and string in our excises). if you want to know more about the other four type, click here! Exercise: Q 2.1 Create variables that used to store the information of yourself, like your name, age, gender, home address and your habits. Q 2.2 Create four variables that could calculate the difference and quotient between two numbers.(Hint: you could use operation"%" to calculate quotient.) Section 3: Using Java Script in HTML In HTML 5, Java Script are instructions to be executed by web browser. In order to display data using Java Script in HTML, you can use window.alert() and innerHTML. You can use window.alert() to display data via alert box. Example 3.1 <html> <body> <h1>My First Web Page <p>My first paragraph.

<script> window.alert(5+6); Tip: All the Java Script code must start with <script> and finish with . All the things between the two script tags is the Java Script code being used in HTML5. The part in side two script tags is used to display information in an alert box. Information we typed in parentheses will be displayed. In this example, the alert box will display the sum of 5 and 6 which is 11. But what if we want to display information in the web page instead of an alert box? We could use innerHTML. Example 3.2 <html> <body> <h1>My First Web Page <p>My first paragraph.

<p id="sayHello">

<script> document.getElementById("sayHello").innerHTML = "Hello World!"; Example 3.2 is a series lines of code for calling the web browser to write Hello world! in the ID sayHello. InnerHTML is a method that writing into an HTML element. And in the line id="sayHello", the HTML called the id "sayHello", and displayed the information stored in the ID "Hello world!". In example 2.1, we write a series of statement that add up two numbers. And if we want to use this in a web page to display the sum of the two numbers, what we need to do is : Example 3.3 (Extension of Example 2.1) <script> var x = 3; var y = 6; var z = x + y; document.getElementById("sum").innerHTML = z; The last line in Example 3.2 means after the above add up, the final value (in these case is the sum which represented by z) will be displayed on the web. Exercise: Q 3.1 Write a HTML file that could say hello to the user in an alter box. Q 3.2 Write a statement that can display your favourite sentence on the web. Q 3.3 Display your favourite sentence on the web page. Q 3.4 Write several statement that could do addition and multiplications of 3 numbers (you could pick three random number you like.), and display the sum and products in the web page. Section 4: Java Script Functions In section 3, Example 3.2 are a function calculate the sum of two numbers, if we want to get both difference and the sum of two numbers, we could do this : Example 4.1 (Extension of Example 3.3) <script> var x = 3; var y = 6; var z = x + y; document.getElementById(“sum").innerHTML = “z"; <script> var x = 3; var y = 6; var m = x - y; document.getElementById(“difference").innerHTML = "m"; The second part of Example 4.1 is a series of statement that calculate difference of two numbers. And after executed the whole code, in the web page you will see the sum and the difference. It seems that it could work properly during this stage. But if we want to calculate any other numbers, we need to repeat those line of code. If you want to calculate the difference and sum of 1000 paris number, you code could be extremely long, which might not be what we want. In order to improve this, what we could do is declare a Java Script Function: Example 4.2 (Improvement of 4.1) <script> var x = 3; var y = 6; function sumAndDifference () { var z = x + y; var m = x - y; document.getElementById(“sum").innerHTML = “z”; document.getElementById(“difference").innerHTML = "m"; } Tip: A new key word Function has shown in 4.2 , this key word is just used for declaring a function that can be used. (x, y) are two parameters we used in function difference and sum, which is separated by "," . Inside the function, 3 and 7 are invoked for "return x-y;". A function will stop, when it reached the return statement. If the invoked parameter ares calculate in return statement it will return the value to the caller. In Example 4.2, the part between "{ }" sign is called a function,.In 4.2 , the two functions is used for calculating sum and difference. Now we do not need to declare variables separately, it will save a lot of space. Exercise: Q 4.1 Write a function that can do multiplication addition for three numbers. Reference: W3schools.com,2014. JavaScript Tutorial [online] Available at: http://www.w3schools.com/js/default.asp [Accessed 11 February 2015]. ← Go back to Index Page Click Here! Back to the Intro Page