Presentation is loading. Please wait.

Presentation is loading. Please wait.

JavaScript Function Example Please use speaker notes for additional information.

Similar presentations


Presentation on theme: "JavaScript Function Example Please use speaker notes for additional information."— Presentation transcript:

1 JavaScript Function Example Please use speaker notes for additional information.

2 Note the difference in execution in Netscape 6.2 and Explorer. The H1 at the beginning of the BODY appears prior to the prompts in Explorer. avgfunca.html

3 Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); avgfunca.html The function is called and the data that was passed in is sent to the function. The data that is sent is defined in the SCRIPT outside the function and is therefore global. The data is received using the same names. The function calculates the average in theAvg and shows the answer.

4 avgfuncaa.html Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams", "0")); var proj = parseInt(prompt("Key in the grade for projects", "0")); var hmwk = parseInt(prompt("Key in the grade for homework", "0")); calAvg(exam, proj, hmwk);

5 avgfuncb.html

6 Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); You should note that when I define theAvg using var this does not work because theAvg in the body does not see it. That is because variables defined within a function are local. If you define them outside the function they are global. Omitting the word var gets around this but is not considered a great coding technique. avgfuncb.html

7 avgfuncc.html

8 Function to calculate average function calAvg(exam, proj, hmwk) { var theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); You should note that when I define theAvg using var in the function, I cannot see the document write of the answer from the script in the Body. That is because variables defined within a function are local. If you define them outside the function they are global. Omitting the word var gets around this but is not considered a great coding technique. avgfuncc.html

9 avgfuncd.html

10 Function to calculate average function calAvg(exam, proj, hmwk) { var theAns = (exam + proj + hmwk)/3; document.write("The average is " + theAns); theAvg = theAns; } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it.

11 avgfunce.html

12 Function to calculate average function calAvg() { var theAns = (exam + proj + hmwk)/3; document.write("The average is " + theAns); theAvg = theAns; } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(); document.write(" The average is " + theAvg); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Note that when I define the variables as I did, outside the function, I do not need to pass. Preferred methodology is to pass. In fact, usually you use separate names to maintain the integrity of the data. avgfunce.html

13 avgfuncf.html

14 Function to calculate average function calAvg(calExam, calProj, calHmwk) { var theAns = (calExam + calProj + calHmwk)/3; document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); theAvg = theAns; document.write(" Last line in the function "); } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Here I am using separate names for the data inside the function and outside the function to maintain the integrity of the data.

15 avgfuncg.html

16 Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); theAvg = theAns; document.write(" Last line in the function "); } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Here I am using separate names for the data inside the function and outside the function to maintain the integrity of the data. I am also changing the value of the data after it has been passed.

17 avgfunch.html

18 Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); document.write(" Last line in the function "); return theAns; } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); theAns = calAvg(exam, proj, hmwk); document.write(" The average is " + theAns); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I am calculating theAns and then returning it. Note I could have defined a var called theAvg here in the BODY script and then assigned theAns to theAvg and displayed theAvg.

19 avgfunci.html

20 Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); document.write(" Last line in the function "); return theAns; } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); document.write(" The average is " + calAvg(exam, proj, hmwk)); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I am calculating theAns and then returning it. Note that I call the function within the write.


Download ppt "JavaScript Function Example Please use speaker notes for additional information."

Similar presentations


Ads by Google