Variety of JavaScript Examples Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Introduction to JavaScript Please see speaker notes for additional information!
TIK-20 Computer and Information Science April 3, TIK20 - West Hill C.I. - Spring 2009.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
2012 •••••••••••••••••••••••••••••••••• Summer WorkShop Mostafa Badr
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Advanced Decisions and Loops Chapter Some Simple Schoolroom Statistics.
Loops Doing the same thing over and over and over and over and over and over…
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
CSS Class 7 Add JavaScript to your page Add event handlers Validate a form Open a new window Hide and show elements Swap images Debug JavaScript.
JavaScript - Introduction Please use speaker notes for additional information!
Getting Started with HTML Please use speaker notes for additional information!
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Logic Structure - focus on looping Please use speaker notes for additional information!
Using Client-Side Scripts to Enhance Web Applications 1.
Client-Side Scripting JavaScript.  produced by Netscape for use within HTML Web pages.  built into all the major modern browsers. properties  lightweight,
Chapter 5 Loops.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
30/10/ Iteration Loops Do While (condition is true) … Loop.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
When I want to work with SQL, I start off as if I am doing a regular query.
Sahar Mosleh California State University San MarcosPage 1 JavaScript Basic.
JavaScript Loops Please use speaker notes for additional information!
JavaScript, Fourth Edition
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Introduction To JavaScript. Putting it Together (page 11) All javascript must go in-between the script tags. All javascript must go in-between the script.
Loops.  (No Quiz)  Hand in Assignment #1  Last chance for Q+A on the midterm  Loops 2.
Review while loops Control variables Example Infinite loop
COMP403 Web Design JAVA SCRİPTS Tolgay KARANFİLLER.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
JavaScript Challenges Answers for challenges
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Project 5: Using Pop-Up Windows Essentials for Design JavaScript Level One Michael Brooks.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 5: Intro to Scripting CIS 275—Web Application Development for Business I.
Working with the Window Object JavaScript considers the browser window an object, which it calls the window object.
CSD 340 (Blum)1 Introducing Text Input elements and Ifs.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Popup Boxes.
1 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.
Learning Javascript From Mr Saem
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
CHAPTER 4 DECISIONS & LOOPS
CHAPTER 10 JAVA SCRIPT.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
BIT116: Scripting Loops.
Java Programming: Guided Learning with Early Objects
Chapter 6: Conditional Statements and Loops
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Please use speaker notes for additional information!
We are starting to program with JavaScript
the captured notes and redid - hopefully it all works.
Programs. at the code at the site..
Using screens and adding two numbers - addda.cbl
Wrapup which is the document write that says the end.
Presentation transcript:

Variety of JavaScript Examples Please use speaker notes for additional information!

document.write(" This is green font "); Note also since I am using double quotes outside. I need to use single quotes or no quotes inside for the color. document.write(" This is blue font "); document.write(" This is orange font "); document.write("Notice that the line break was put inside the quotes "); document.write("No line break means no break"); document.write("Also note that the semi-colon is between commands");

Changing pages <input TYPE="button" VALUE="Bristol" onClick="window.location=' <input TYPE="button" VALUE="Grocer" onClick="window.location='

Changing pages <a HREF="#" onClick="window.open(' 'grocer', 'height=500, width=500,scrollbars,resizable,status,toolbar,menubar'); return false;"> <a HREF="#" onClick="window.open(' 'bcc', 'height=500, width=500,scrollbars,resizable');"> The syntax for the open method is window.open(“URL”, “name”, options) In the example above grocer and bcc are names and the options are shown.

<a HREF="#" onClick="window.open(' 'grocer', 'height=500, width=500,scrollbars,resizable,status,toolbar,menubar'); return false;"> Note that the window that was opened has scrollbars, status bar etc based on the options specified.

a HREF="#" onClick="window.open(' 'bcc', 'height=500, width=500,scrollbars,resizable');"> Note that this only specifies scrollbars and resizable so the menu bars etc are missing.

Another open test function openPGrocerNet() { window.open(" } Testing...Testing...Testing...Testing...Testing...Testing...Testing... Click here to go to pgrocer.net Note, when I click on this it brings up the pgrocer.net in the current page and the CIS44 page in the small window. If I had both pointing to the CIS44 page it would change the current window to CIS44 and bring up the new window with CIS44. See the next slide.

Another open test function openPGrocerNet() { window.open(" } Testing...Testing...Testing...Testing...Testing...Testing...Testing... Click here to go to pgrocer.net

Another open test function openPGrocerNet() { window.open(" } Testing...Testing...Testing...Testing...Testing...Testing...Testing... Click here to go to pgrocer.net

Working with a table document.write(" This will show contents of a table! "); document.write(" "); document.write(" First, First "); document.write(" First, Second "); document.write(" Second, First "); document.write(" Second, Second "); document.write(" Third, First "); document.write(" Third, Second "); document.write(" "); The table is complete!

Function to calculate average function addIt() { x = ; alert(x); } Click here Note that JavaScript:functionName() can be used with href to give the location parameter that is associated with the anchor tag's href attribute.. In this case the function is alert. Click here again function addIt() { x = ; alert(x); }

function addIt() { x = ; alert(x); }

This is the while loop... var data_input=prompt("How many times should I write the line?",0) ct=1 while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } document.write(" " + "This is the end!!!");

var data_input=prompt("How many times should I write the line?",0) ct=1 while (ct <= data_input) { document.write("This is number " + ct + " "); ct = ct + 1; } document.write(" " + "This is the end!!!"); data_input 5 ct Pass 1 through the while loop: ct = 1 & data_input = 5 so ct is <= data_input & the loop is entered This is number 1 is written since ct is 1 ct is incremented by 1 so ct is now 2 Pass 2 through the while loop ct = 2 & data_input = 5 so ct is <= data_input & the loop is entered This is number 2 is written since ct is 2 ct is increment by 1 so ct is now 3 Pass 3 through the while loop ct = 3 & data_input = 5 so ct is <= data_input & the loop is entered This is number 3 is written since ct = 3 ct is incremented by 1 so ct is now 4 Pass 4 through the while loop ct = 4 & data_input = 5 so ct <= data_input & the loop is entered This is number 4 is written since ct = 4 ct is incremented by 1 so ct is now 5 Pass 5 through the while loop ct = 5 & data_input = 5 so ct <= data_input & the the loop is entered This is number 5 is written since ct = 5 ct is incremented by 1 so ct is now 6 Pass 6 ct = 6 & data_input = 5 so ct is not <= data_input and the loop is not entered

This is the while loop... //The do...while executes the statements in the loop ones and then repeats the //the execution while the condition is true. This means the do...while executes //at least once. This is the difference between the do...while and the while that //we looked at earlier. The while evaulates the condition before executing so the //statements in the loop may never be executed. var data_input=prompt("How many times should I write the line?",0) ct=1 do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input) document.write(" " + "This is the end!!!"); When I was prompted for the number of lines, I entered 4. Which means I see This is a number… written 4 times.

data_input 4 ct Pass 1 through the do...while loop: This is number 1 is written since ct is 1 ct is incremented by 1 so ct is now 2 while checks: ct = 2 & data_input = 4 so ct is <= data_input & the loop is reentered Pass 2 through the do...while loop: This is number 2 is written since ct is 2 ct is incremented by 1 so ct is now 3 while checks: ct = 3 & data_input = 4 so ct is <= data_input & the loop is reentered Pass 3 through the do...while loop: This is number 3 is written since ct is 3 ct is incremented by 1 so ct is now 4 while checks: ct = 4 & data_input = 4 so ct is <= data_input & the loop is reentered Pass 4 through the do...while loop: This is number 4 is written since ct 4 ct is incremented by 1 so ct is now 5 while checks: ct = 5 & data_input = 4 so ct is NOT <= data_input so the loop is not reentered var data_input=prompt("How many times should I write the line?",0) ct=1 do { document.write("This is number " + ct + " "); ct = ct + 1; } while (ct <= data_input)

Math Facts MATH FACTS 1..3 for (i=1; i<=3; i=i+1) { ans=i + i; document.write(i + "+" + i + "=" + ans + " "); }

for (i=1; i<=3; i=i+1) { ans=i + i; document.write(i + "+" + i + "=" + ans + " "); } i1234i1234 Before the loop is started, i is set to 1. First pass through the for loop: ans = i + i means that since i is 1, ans is 2 write the line = 2 Before the second pass is started: i is incremented by 1 so i is now 2 and i is checked to see if it is still less then or equal to 3 before the loop is entered Second pass through the loop: ans = i + i means that since i is 2, ans is 4 write the line = 4 Before the third pass is started: i is incremented by 1 so it is now 3 and i is checked to see if it is still less than or equal to 3 before the loop is entered Third pass through the loop: ans = i + i means that since i is 3, ans is 6 write the line = 6 Before another pass is started: i is incremented by 1 so it is now 4 and is is checked to see if it is still less than or equal to 3. It is NOT so the loop is NOT ENTERED

Math Facts MATH FACTS 1..3 for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); }

for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); } i j ans Outer for loop starts and i is set to 1. Inner for loop starts and j is set to 1. Inside inner loo: ans=1+1 write = 2 Loop back to inner loop and increment j by 1. It is now 2. Check to see if j which is 2 is <= 3. It is so enter loop. ans = write = 3 Loop back to inner loop and increment j by 1. It is now 3. Check to see if j which is 3 is <= 3. It is so enter loop. ans = write = 4 Loop back to inner loop and increment j by 1. It is now 4. Check to see if j which is 3 is <= 3. It is NOT so drop out of loop and loop back to outer loop. In outer loop, increment i by 1 making it 2. Check to see if i which is 2 is <=3. It is so drop down to inner loop. Since you dropped down, reset j = 1 and enter the inner loop. ans = write = 3 1+1=2 1+2=3 1+3=4 2+1=3 2+2=4 2+3=5 3+1=4 3+2=5 3+3=6

for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); } i j ans The last thing we put out was = 3 where i was 2 and j was 1. Loop back to inner loop and increment j by 1. It is now 2. Check to see if j which is 2 is <= 3. It is so enter loop. ans = write = 4 Loop back to inner loop and increment j by 1. It is now 3. Check to see if j which is 3 is <= 3. It is so enter loop. ans = write = 5 Loop back to inner loop and increment j by 1. It is now 4. Check to see if j which is 3 is <= 3. It is NOT so drop out of loop and loop back to outer loop. In outer loop, increment i by 1 making it 3. Check to see if i which is 3 is <=3. It is so drop down to inner loop. Since you dropped down, reset j = 1 and enter the inner loop. ans = write = 4 1+1=2 1+2=3 1+3=4 2+1=3 2+2=4 2+3=5 3+1=4 3+2=5 3+3=6

for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + ans + " "); } i j ans The last thing we put out was = 4 where i was 3 and j was 1. Loop back to inner loop and increment j by 1. It is now 2. Check to see if j which is 2 is <= 3. It is so enter loop. ans = write = 5 Loop back to inner loop and increment j by 1. It is now 3. Check to see if j which is 3 is <= 3. It is so enter loop. ans = write = 6 Loop back to inner loop and increment j by 1. It is now 4. Check to see if j which is 3 is <= 3. It is NOT so drop out of loop and loop back to outer loop. In outer loop, increment i by 1 making it 4. Check to see if i which is 4 is <=3. It is NOT so drop out of the outer loop - PROCESSING IS COMPLETE 1+1=2 1+2=3 1+3=4 2+1=3 2+2=4 2+3=5 3+1=4 3+2=5 3+3=6

I entered 2 and got confirmation that the answer was correct Now I have entered 3 to the question = After I click OK I get confirmation that = 3 I entered the wrong answer for 1+3= so I got a message saying No and giving me the correct answer. Now I am being prompted for the answer to =

This shows the complete math facts shown by this program. The last problem is =

Math Facts MATH FACTS 1..3 for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); }

Click on cancel and the processing will end. The output stopped at this point.

Math Facts MATH FACTS 1..3 // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set i to 4 to end the outer // loop - some programmers would not choose this approach because they avoid this // kind of manipulation for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { i=4; break; }

Math Facts MATH FACTS 1..3 // If the user presses cancel the input is null - this program tests for null // Break exits out of the closest loop - note that I then set endFlag to Y and // when I return to the outer loop I test to see if the endFlag is Y which // means I want to break out of that loop as well // Note also that null and empty are not the same thing = "" is empty In this example I am using a variable that I name endFlag. Remember a break only breaks out of the current loop, so when I encounter the need to break I set the endFlag to Y. I can then test the endFlag back in the outer loop to see if I want to break out of that loop as well.

var endFlag = "N" for (i=1; i<=3; i=i+1) { for (j=1; j<=3; j=j+1) { ans=i + j; document.write(i + "+" + j + "=" + " "); var user_input = prompt("What is your answer?",0); if (user_input != null) { if (ans == user_input) { document.write("Yes, the answer is " + ans + " "); } else { document.write("No, the answer is " + ans + " "); } else { endFlag="Y"; break; } if (endFlag == "Y") { break; } I am in the inner loop taking in user input - I want to check and see if the user clicked Cancel. If they did the user_input will be null. If the user clicked Cancel the test for if (user_input != null) will be false since the user_input is null. That means we will drop down to the else and set the endFlag = “Y” and break out of the inner loop. Set endFlag to “N” before processing I have now dropped out of the inner loop because of the break. Before returning to the outer loop, I test the endFlag to see if it is equal to Y, if it is I break which breaks out of the outer loop.