Presentation is loading. Please wait.

Presentation is loading. Please wait.

BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES

Similar presentations


Presentation on theme: "BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES"— Presentation transcript:

1 BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
CSC317 – INTERNET PROGRAMMING CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES

2 Outline Basic JavaScript For Loop While Loop Do While Loop
User-defined Function Error Checking Working with Web Pages Properties Page 2

3 Basic JavaScript for Loop
Execute the same block of code according specified times for(var = startValue; var <= endValue; var = var + increment){ // execute the block code until var = endValue } var x; for(x = 0; x <= 10; x++){ document.write(“Number: ” + x + “<br>”); } Page 3

4 Basic JavaScript for Loop Another example on for loop var x;
var msg = “I promise I won’t be disturb people again.”; for(x = 1; x <= 1000; x++){ document.write(msg + “<br>”); } Page 4

5 Basic JavaScript while Loop
Execute the same block of code while a specific condition is true while(var <= endValue){ // execute the code } var x = 0; while(x <= 10){ document.write(“Number: ” + x + “<br>”); x++; // x = x + 1 } Page 5

6 Basic JavaScript while Loop Another example on while loop
var month_name, month = 1; while(month <= 3){ if(month == 1){ month_name = “January”; } else if(month == 2){ month_name = “February”; else if(month == 3){ month_name = “March”; else{ alert(“Please select month!”); month = month + 1; Page 6

7 Basic JavaScript do...while Loop
Execute the block of code at least once even the condition is false, because the code will be executed first before condition is tested. It will repeat the loop as long as the specified condition is true. do{ // execute the code } while(var <= endValue) var x = 0; do{ document.write(“Number is: ” + x + “<br>”); x = x + 1; } while(x < 0) Page 7

8 Basic JavaScript Question 1: for Loop
Write a JavaScript code that will generate similar output in Figure 1. Please use for loop. Figure 1 Page 8

9 Basic JavaScript Question 2: for Loop
Write a JavaScript code that will generate similar output in Figure 2. Please use for loop. *calculate the value within your loop Figure 2 Page 9

10 Basic JavaScript User-defined Function
A function created by user to perform specific task <html> <head> <script type="text/javascript"> function myFunction(){ var msg = "Hello World!"; return (msg); } </script> </head> <body> document.write(myFunction()); </body> </html> Page 10

11 Basic JavaScript User-defined Function function xx(){ }
<script language="javascript“> function kira(){ var num1=parseFloat(document.f1.num1.value); var num2=parseFloat(document.f1.num2.value); var operator=document.f1.operator.value; var sum; if(operator=="add"){ sum=num1+num2; } else if(operator=="minus"){ sum=num1-num2; else if(operator=="multiple"){ sum=num1*num2; else if(operator=="division"){ sum=num1/num2; else{ alert("select an operator"); document.f1.result.value=sum; </script> Basic JavaScript User-defined Function function xx(){ } Page 11

12 Basic JavaScript User-defined Function return Statement Example
The return statement is used to specify the value that is returned from the function. So, functions that are going to return a value must use the return statement. The example below returns the product of two numbers (a and b): Example <html> <head> <script type="text/javascript"> function product(a,b){ return a*b; } </script> </head> <body> <script type="text/javascript"> document.write(product(4,3)); </script> </body> </html> Page 12

13 Basic JavaScript Error Checking Remember this example? <html>
<head> <script type="text/javascript"> function myFunction(){ var fname = document.form1.fname.value; alert("Name: " + fname); } </script> </head> <body> <form name="form1"> Name: <input type="text" name="fname"> <input type=“button" name="submit" value="Submit" onclick="myFunction()"> </form> </body> </html> Page 13

14 Basic JavaScript Error Checking
myFunction() will capture a value from fname text field. This field is specifically for people´s name, which is string value What will happen if user enter numeric value? Should myFunction() accept the value without need to do error checking? What will happen if user does not enter any value? The field leave blank. <script type="text/javascript"> function myFunction(){ var fname = document.form1.fname.value; alert("Name: " + fname); } </script> Page 14

15 Basic JavaScript Error Checking <script type="text/javascript">
function myFunction(){ var fname = document.form1.fname.value; if(fname == ""){ // check if no value being entered alert("Please enter a name!"); } else if(fname == null){ // check if there is undefined value else if(!isNaN(fname)){ // check if value is numeric alert("Invalid character. Name must be alphabet."); else{ // all conditions above false document.write("Name: " + fname); </script> Page 15

16 Basic JavaScript Error Checking
fname = ″″ and fname = null are two different statements fname = ″″ : check if the value is empty fname = null : check if the value is null (undefined or unknown) Page 16

17 Basic JavaScript Question 3: for Loop
Write a JavaScript code that will generate similar output in Figure 3. Within your form A text field for user input A button to trigger a function to check the input as numeric input to generate times calculation based on the input the calculation will start from 1 until 12. output a list of the times calculation (you can used two for loop in your coding) A button to reset the value Page 17

18 Basic JavaScript Question 3: for Loop Figure 3 Page 18

19 Basic JavaScript Question 4: for Loop
Write a JavaScript code that will generate similar output in Figure 4. Within your form A text field for user input set maximum length of user input as 2 value A button to trigger a function to check the input as numeric input to check the input either odd or even output a list of different odd and even number . (you can used two for loop in your coding) A button to reset the value Page 19

20 Basic JavaScript Question 4: for Loop Figure 4 Page 20

21 Basic JavaScript Question 5: for Loop & if Statement
Create a Web page embedded with JavaScript code that accepts an integer in a textbox. After the user clicks on the button, it then displays the output, which is sum of the even digits from 1 to the positive integer inserted. The output will be in an alert box. Use if statement to determine the even numbers. Then use for loop technique in order to calculate the sum of the even numbers. A sample output is shown below: Figure 5

22 Basic JavaScript Question 6: for Loop & if Statement
Create a Web page embedded with Javascript code that accepts a positive integer in a textbox. After the user clicks on the button, it then displays the output as a series of six numbers in a textarea. Use for loop in order to calculate the value of the series. A sample output is shown below: Figure 6

23

24 Basic JavaScript Working with Web Pages Properties
May be you are not realized that you have used Web Pages properties (or Document properties) a few times Example: document.write(): to display information on the web page We have others like: document.bgColor document.fgColor document.lastModified Page 24

25 Basic JavaScript Working with Web Pages Properties document.bgColor

26 Basic JavaScript Working with Web Pages Properties document.bgColor
<html> <head> <script language="JavaScript"> function changeBG(color){ document.bgColor=color; //Change background color document.f1.color.value=color; //Display the color } </script> </head> <body> <form name="f1"> <input type="button" value="OceanBlue" name="Yellow" onClick="changeBG('#94FFFF')"> <input type="button" value="LimeGreen" name="Green" onClick="changeBG('#B8FF94')"> <input type="text" name="color"> </form> </body> </html> Page 26

27 Basic JavaScript Working with Web Pages Properties document.fgColor
<html> <head> <title>document.fgcolor</title> </head> <body bgcolor="beige" text= "red" link= "green" vlink="honeydew"> <script language="javascript"> document.write("the text color is "+document.fgColor+"<br>"); </script> </body> </html> Page 27

28 Basic JavaScript Working with Web Pages Properties
document.lastModified <html> <head> <title>document.lastModified</title> </head> <body> This document was last modified on: <script type="text/javascript"> document.write(document.lastModified); </script> </body> </html> Page 28

29 Bibliography (Website)
Bibliography (Book) Knuckles (2001). Introduction to Interactive Programming on the Internet using HTML & Javascript. John Wiley & Sons, Inc. Bibliography (Website) Page 29 29


Download ppt "BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES"

Similar presentations


Ads by Google