230 likes | 350 Views
This resource aims to demystify programming for beginners by utilizing JavaScript without the need for any software installation. With just a text editor and a web browser, students can quickly explore fundamental programming concepts like variables, arrays, loops, and conditional statements. The simplicity and instant feedback of JavaScript make it an ideal choice for novice programmers. This guide includes practical examples and clear instructions, facilitating a smooth learning experience in a user-friendly environment.
E N D
Teaching Programming Conceptswith JavaScript: No SoftwarePackage Required Paul Addison, Ivy Tech Community College Lafayette, Indiana
What scares off beginning programming students? • Software installation problems • Confusion about files and directories • Complexity of IDE • IF and WHILE statements? Not so much!
Simplicity of JavaScript • No software installation • Any text editor works • No compiler needed • Runs in any browser • No Internet connection required • Instant feedback
Three HTML tags: that’s all <html> <body> <script type=“text/javascript”> (JavaScript statements go here) </script> </body> </html>
JavaScript output goesto the browser <html> <body> <script type=“text/javascript”> // Display statements write to the browser page document.write(“Do I have to say Hello, World?”); </script> </body> </html>
Create and save the file in Notepad with an .html extension <html> <body> <script type=“text/javascript”> // Display statements write to the browser page document.write(“Do I have to say Hello, World?”); </script> </body> </html>
Quickly move toprogramming concepts <html> <body> <script type=“text/javascript”> // Declare variables, add and display numbers var numBoys = 14; var numGirls = 16; var totStudents = numBoys + numGirls; document.write(“Total students: ” + totStudents); </script> </body> </html>
The 3 programming structures <html> <body> <script type="text/javascript"> // Name of program: structures.html // Purpose of program: presents the three programming structures // Author: Paul Addison // Date: 15-Jan-2010 // Declare constants and variables var SP = " "; // literal space var BR = "<br />;" // line break var number; // number used for selection and loop // This section demonstrates sequence document.write("These first three statements are in sequence." + BR); document.write("They follow one another in order." + BR); document.write("There is no variation or repeating." + BR); // This section demonstrates selection number = 5 if (number < 10) document.write(number + " is less than 10." + BR); else document.write(number + " is greater than or equal to 10." + BR); // This section demonstrates looping while (number <= 20) { document.write(number + SP); number = number + 1 } </script> </body> </html>
Arrays <html> <body> <script type="text/javascript"> // Name of program: displayArray.html // Purpose of program: set up an array, and display it forwards and backwards // Author: Paul Addison // Date: 15-Jan-2010 // Declare constants var BR = "<br />"; // line break // Declare and initialize array of movie titles var movieTitles = new Array(8); movieTitles[0] = "Gone With the Wind"; movieTitles[1] = "Finian's Rainbow" movieTitles[2] = "Camelot"; movieTitles[3] = "Easy Rider"; movieTitles[4] = "Hatari"; movieTitles[5] = "Goldfinger"; movieTitles[6] = "Swiss Family Robinson"; movieTitles[7] = "Ben Hur"; // Display the array elements in forward order // Subscripts go from 0 up to 7 for (i=0; i<=7; i++) { document.write(movieTitles[i] + BR); } document.write(BR); // Display the array elements in backwards order // Subscripts go down from 7 to 0 for (i=7; i>=0; i--) { document.write(movieTitles[i] + BR); } document.write(BR); </script> </body> </html>
<html> <body> <script type="text/javascript"> // Name of program: bubbleSort.html // Purpose of program: use bubble sort logic on an array // Author: Paul Addison // Date: 15-Jan-2010 // Declare constants and variables var BR = "<br />"; // line break var ARRAYSIZE = 3; // size of array var maxElement; // last element in current array pass var index; // array index // Declare and initialize the array var presidentName = new Array(ARRAYSIZE); presidentName[0] = "Washington"; presidentName[1] = "Kennedy"; presidentName[2] = "Lincoln"; // Display the array before sorting document.write(BR + "Before sorting:" + BR); for (i = 0; i < ARRAYSIZE; i++) { document.write(presidentName[i] + BR); } // Bubble sort logic // Outer loop works from last array element down to the first // Inner loop steps through array, comparing and swapping if necessary for (maxElement = ARRAYSIZE - 1; maxElement >= 0; maxElement--) { for (index = 0; index < maxElement; index++) { if (presidentName[index] > presidentName[index + 1]) { temp = presidentName[index]; presidentName[index] = presidentName[index + 1]; presidentName[index + 1] = temp; } } } // display the array after sorting document.write(BR + "After sorting:" + BR); for (i = 0; i < ARRAYSIZE; i++) { document.write(presidentName[i] + BR); } </script> </body> </html> Sorting algorithms
Prompting and calling a function <html> <head> <script type="text/javascript"> // Name of program: squareFunction.html // Purpose of program: uses a function to compute and return a square // Author: Paul Addison // Date: 15-Jan-2010 // This function takes an argument and squares it function square(num1) { document.write("The argument sent to the square function was " + num1 + BR); var result = num1 * num1; return (result) } </script> </head> <body> <script type="text/javascript"> // Declare constants and variables var BR = "<br />"; // line break var ES = ""; // empty string var num; // number entered by user var numSquared; // square of number, returned by function // Prompt the user for a value between 15 and 25, convert to an integer num = prompt("Please enter a number from 15 to 25: ",ES); num = parseInt(num); document.write("You entered the number " + num + BR); // Input validation: reprompt the user to enter a number between 15 and 25, as long as necessary while (num < 15 || num > 25) { num = prompt("Error...Please enter a number from 15 to 25: ",ES); num = parseInt(num) } // Call the square function, display the result var numSquared = square(num); document.write("The value returned to the main program was " + numSquared + BR); document.write("End of program." + BR); </script> </body> </html>
Even recursion! <html> <head> <script type="text/javascript"> // Name of program: factorial.html // Purpose of program: use recursion to calculate a factorial // Author: Paul Addison // Date: 15-Jan-2010 // This function implements the definition of a factorial // If the number is 1, the function returns the value 1 // Otherwise, it calls itself with the argument of the number - 1 function factorial(num) { document.write("Processing the factorial of: " + num); if(num==1) return(1); else return(num * factorial(num-1)); } </script> </head> <body> <script type="text/javascript"> // Declare constants and variables var BR = "<br />"; // line break var ES = ""; // empty string var num; // the number entered by the user var numFactorial; // the factorial calculated by the function // Prompt the user for a number, convert the input to an integer // Call the factorial function, and display the result num = prompt("Enter a number: ",ES); num = parseInt(num); numFactorial = factorial(num); document.write("The factorial of " + num + " is " + numFactorial + BR); </script> </body> </html>
Summary • Easy to use • Almost no overhead • Syntax carries easily from pseudocode, to C# and Java • You can teach good programming style and techniques • You can teach programming concepts, not software!
Teaching Programming Conceptswith JavaScript: No SoftwarePackage Required Paul Addison, Ivy Tech Community College Lafayette, Indiana