AP Digital Edge Training - Computer Science A & JavaScript: Jeannie Turner & John Meinzen

Home | Thur, 8July | Fri, 9July | Sat, 10July

Morning Session I : Lessons 1, 2, & 3 [9:30-11:00am]

Lesson 1 - Introduction to JavaScript [20min] L14-L35 - John
[Mark start of each Lesson in Leader's Handbook]

  • Stage 1: Plan L14-L16

    Stage 2: Teach L17-L30

    • Anticipatory Set - builds a "Real-life story" for motivation and initial discussion with students. See https://javascript.com and complete first few steps

    • Modeling

      • Activity 1 - find a website that uses JavaScript and/or provide direct instruction to students.

      • Activity 2 - research other interpreted computer languages & Review General Terms & Concepts (L21-23)

      • Activity 3 - L24, Java verus JavaScript Venn Diagram on page 13 (L19)

      • Activity 4 - L24-25 - Students will write their first HTML + JavaScript

        • Additional reference resource at www.w3schools.com

        • see Guided Practice...focus on :

          HTML tags and attributes such as <script type="text/javascript" src="example.js"></script>

          commenting,

          deprecated code,

          differences in browsers, JavaScript versions, device differences (desktop vs smartphone)

    • Checking for Understanding - L27, (Student Worksheet page 15)- commenting

    • Guided Practice - L29 Lesson 1 Lab.htm...edit by removing HTML commenting and JavaScript commenting around the document.write(...) code.

    Stage 3: Assess L31-L34 APCS-A style questions in Java...could have students re-write in JavaScript

    Stage 4: Act - Supplemental Resources

Lesson 2 - Variables and Data [40min] L36-L51 - John

Lesson 3 - Functions and Methods [30min] L52-L63 - Jeannie

  • Stage 1: Plan - http://mathstretch.com/js/Lesson3.html

    Stage 2: Teach

    • Anticipatory Set

    • Modeling

    • Checking for Understanding

    • Guided Practice

    Stage 3: Assess

    • Assessments

    • Independent Practice

    Stage 4: Act - Supplemental Resources

Morning Session II : Lessons 3 & 4 [11:15-1:00pm]

Lesson 3 - Functions and Methods [continued] - Jeannie

...see Morning Session 1

Lesson 4 - Program Flow L64-L70 - Jeannie

  • Stage 1: Plan http://mathstretch.com/js/Lesson4.html

    Stage 2: Teach

    • Anticipatory Set

    • Modeling

    • Checking for Understanding

    • Guided Practice

    Stage 3: Assess

    • Assessments

    • Independent Practice

    Stage 4: Act - Supplemental Resources

Afternoon Session I : Lesson 5 [2:00 - 3:30pm]

Lesson 5 - Introduction to standard Java and JavaScript DOM L71-L83 - John

  • Stage 1: Plan -

    • The lesson contains one complete HTML file and one complete JavaScript file. As a first approach, have students type in each file and re-create the working dynamic webpage.

    • Teachers should demonstrate the two files and point out the connection between HTML id attributes which generates objects in the DOM and the built-in JavaScript document.getElementById() method that finds those objects.

    • There are seven id attributes in the HTML - 1) id="headingIdRed", 2) id="promptIdRed", 3) id="perscentIdRed", 4) id="inputIdRed", 5) id="numberIdRed", 6) id="lowIdRed", and 7) id="highIdRed"

    • Browsers vary on how they implement the DOM. In this Lesson, teachers must be prepared to discuss (but not necessarily solve) DOM compatibility issues. The X/HTML and JavaScript work more completely in more "standards-compliant" browsers. Mozilla Firefox is one such browser.

    Stage 2: Teach - using a dynamically-generated color "progress" bar is a popular graphic on a webpage

    • Anticipatory Set - L75-76 : Note the list comparing JavaScript and Java data types; READ the last two paragraphs out loud.

    • Modeling - see Guided Practice for teacher demonstration & discussion

    • Checking for Understanding - student should generate a list (page 42) of predefined variables/methods in the DOM, JavaScript reserved words, programmer-defined variables/methods

    • Guided Practice - L79-81, printed copies of Lesson5.htm, lesson5.js (or lesson5noComments.js)

      Lesson5.htm

      <!-- <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Lesson 5 - Using the JavaScript with the Document Object Model</title> <style> table {width:90%; margin:10px; margin-left:5%; border:thin} #highIdRed {background-color: lightgray; height:1em} #lowIdRed {background-color: red;} </style> <script src="lesson5.js"></script> </head> <body onLoad="myMainMethod()"> <p >Lesson 5 - Using the JavaScript with the Document Object Model </p> <h3 id="headingIdRed">What shade of red do you want? </h3> <label id="promptIdRed"></label> <input type="text" id="inputIdRed"> <table> <tr> <td id="percentIdRed" width="50%">Red at <td> <td id="numberIdRed" width="50%">Number of red shades attempted: </td> </tr> </table> <table> <tr> <td id="lowIdRed" > </td> <td id="highIdRed"> </td> </tr> </table> </body> </html> -->

      lesson5.js

      <!-- // Lesson 5, JavaScript and the Document Object Model // Good programming practice includes following strict programming guidelines // "use strict" also helps find Syntax Errors that would otherwise fail silently "use strict" /* Global (aka window) variables are initialized in the initRed() method. */ var currentState; // The currentState holds the "model" var userInterface; // The userInterface represents the "view" /* * There are 6 programmer-defined methods given below. * 1. The myMainMethod() is the first method called by the browser AFTER the web page has completed loading * and the DOM has been created in the browser's memory. Corresponds to the tag in the X/HTML * <body onLoad="myMainMethod()">... * 2. The init() method starts by defining and initializing the: * a) programmer-defined "view" object for the user interface (i.e. X/HTML DOM), * b) programmer-defined "model" object representing the user's choice of color. * c) methods to be called when the user makes changes (i.e. hits back button on browser or types a value * d) changes the X/HTML web page via the DOM * 3. The setHistory() method puts the "model" (i.e. new color choice) into the history object which is predefined in the DOM * 4. The getHistoryState() method retrives the old "model" (i.e. color choice) from the history object when the back button is clicked. * 5. The handleNewRedShade() method checks the the user's new color choice to make sure the value is valid before putting it in the "model." * 6. The modifyWebPage() method combines the "view" (DOM) and the "model" (user's color choice or color history) and updates the webpage. */ function myMainMethod() { // Just call the programmer-designed init() method. init(); } /* * The init() method sets up the local object variables for the user interface (i.e. X/HTML) as well * as the methods below that are to be called when the user either types in a new value or clicks on the back button. */ function init(newShade) { /* * Set up an programmer-defined object with variables/properties to match each X/HTML tag id attribute of the * DOM. We use these as the interface between want to store in the history element of the document. */ userInterface = { headingIdRed: null, promptIdRed: null, inputIdRed: null, percentIdRed: null, numberIdRed: null, lowIdRed: null, highIdRed: null }; // Get each of the DOM elements with matching id's and store them in the userInterface array for (var id in userInterface) { // The programmer-defined userInterface object can be accessed just like an Array // but with Strings instead of integers as the key/index. // JavaScript Array objects are similar to Java's Map<String,String> class. // The document.getElementById() is pre-defined in the DOM. userInterface[id] = document.getElementById(id); } /* * Set up another programmer-designed object with variables/properties that are needed to maintain * the state (or condition) each time a user types in a new value for red. * Start with a random red value between 0 and 100. */ currentState = { valueRed: Math.floor(Math.random()*100)+1, // 0 < value of Red <= 100 numberOfTriesRed: 0, // how many shades of red attempted previousRed: undefined // last shade of red entered }; // If the user types in a new red percentage in the input textbox, // then call the handleNewRedShade() method. NOTE: methods are variables in JavaScript and don't always need () at the end! // NOTE: The onchange property (or variable) is predefined in the DOM for the <input type="text"> tag. userInterface.inputIdRed.onchange = handleNewRedShade; // If the browser's back button was clicked (i.e. window), then the history must be accessed and // we call the getHistoryState(event) method. // NOTE: The window.onpopstate property/variable is prebuilt in the DOM as soon as the X/HTML web page starts loading. window.onpopstate = getHistoryState; // Call the programmer-defined modifyebPage() method to change the X/HTML document to display the initial state. modifyWebPage(currentState); } /* * The programmer-defined setHistory() method saves the old values (state) in the history object using * the pushState() method predefined in the history object of the DOM. The history object is also predefined. */ function setHistory(oldState) { // If the document doesn't have a history then just return because we cannot save the old data. if (!history.pushState) { alert ("cannot push to the history object"); return; } // Otherwise put the old state (previousRed) in the url and in the history object. var url = "#previous" + oldState.previousRed; history.pushState(oldState, "", url); } /* * The programmer-defined getHistoryState() method is called whenever the browser's BACK button is clicked * as commented above in the init() method. * The method retrieves the state of the history event. * Both history and event are predefined in the window object in the DOM. */ function getHistoryState(event) { // If the event already has an old state then restore the old state and updated the web page. if (event.state != null) { currentState = event.state; modifyWebPage(currentState); } // Else the event doesn't have an old state (i.e. first time opening the web page) // and replaces the history state with the current state. The replaceState() method // is predefined in the history object of the DOM. else { history.replaceState(currentState, "", "#previous" + currentState.previousRed); } } /* * The programmer-defined handleNewRedShade() method is called whenever the user types in a new value in the textbox * in the browser. */ function handleNewRedShade() { // Get the user's value typed into the input text box and convert it to an integer. // The "this" variable is predefined and is usually synonymous with the window object of the DOM. var value = parseInt(this.value); if ((value > 0) && (value <= 100)) { // Save the current value, update the new value and number of user entries. currentState.previousRed = currentState.valueRed; currentState.valueRed = value; currentState.numberOfTriesRed++; // Save the information in the history object in case the "BACK" button was clicked. setHistory(currentState); // Update the X/HTML DOM to reflect the new values in the browser. modifyWebPage(currentState); } else { alert("Please enter a number between 0 and 100"); // The alert() method is predefined in the DOM. } } /* * The programmer-defined modifyWebPage() method changes the DOM to via each tag's id attribute which is matched * to the userInterface object defined in the initRed() method. */ function modifyWebPage(newState) { document.title = "Try a shade between 0 and 100"; // The document is predefined in the DOM userInterface.headingIdRed.innerHTML = document.title; // Each innerHTML is predefined in the DOM for any X/HTML tag. userInterface.percentIdRed.innerHTML = "Red at " + newState.valueRed +" %"; userInterface.numberIdRed.innerHTML = "Number of red shades attempted: "+newState.numberOfTriesRed; // Convert the red value from a percentage (%) to a byte value between 0 < byteColorRed < 256 // The new Number() and toFixed() methods are predefined as part of JavaScript Number wrapper type. var byteColorRed = new Number(newState.valueRed/100.0 * 255); byteColorRed = byteColorRed.toFixed(0); // Set the value of the background color to the converted value of red entered. // See below the line for a NOTE regarding setting colors for background styles. // The style is predefined in the DOM for any X/HTML tag that can by styled by CSS (cascading style sheets). // THe background-color and rgb() properties are predefined attribute for many X/HTML tags. userInterface.lowIdRed.style = "background-color:rgb("+byteColorRed+",0,0)"; // NOTE: Some browsers may have difficulty with the above statement for setting the color style. // The next line (along with the 'helper' methods below may be used instead. //userInterface.lowIdRed.style.backgroundColor = rgbToHexColor(byteColorRed,0,0); // Modify the table to simulate a completion bar with corresponding color % and width %. userInterface.lowIdRed.style.width = newState.valueRed +"%"; userInterface.highIdRed.style.width = (100-newState.valueRed) + "%"; // Blank the input values and put the cursor focus back on the input text tag. // Both value and focus() are predefined properties of <input type="text"> tags. userInterface.inputIdRed.value = ""; userInterface.inputIdRed.focus(); // Depending on the user entering of values, change prompt appropriately. if (newState.previousRed === undefined) { userInterface.promptIdRed.innerHTML = "Type in a shade of RED and hit Enter"; } else if (newState.previousRed > 100) { userInterface.promptIdRed.innerHTML = "too high, try a number less than or equal to 100"; } else if (newState.previousRed < 0) { userInterface.promptIdRed.innerHTML = "too low, try a number greater than 0"; } else { userInterface.promptIdRed.innerHTML = "Enter a new shade of RED"; } } /** * The following two functions are "helper" functions to convert color integer values into * hexidecimal values that browsers use for styling purposes. * * THE CONVERSIONS ARE BEYOND THE SCOPE OF LESSON 5 */ function rgbToHexColor(red,green,blue) { return "#" + byteToHex(red) + byteToHex(green) + byteToHex(blue); } function byteToHex(value) { var hexidecimalCharacters = "0123456789ABCDEF"; var grabLeft4Bits = value >> 4; var getLeftHexDigit = grabLeft4Bits & 0x0F; var firstHexCharacter = hexidecimalCharacters.substr(getLeftHexDigit,1) var getRightHexDigit = value & 0x0F; var secondHexCharacter = hexidecimalCharacters.substr(getRightHexDigit,1) return String(firstHexCharacter + secondHexCharacter) ; } -->

      lesson5noComments.js- sometimes it is difficult to see the forest for the trees

      <!-- // Lesson 5, JavaScript and the Document Object Model NO COMMENT VERSION "use strict" var currentState; var userInterface; function myMainMethod() {init();} function init(newShade) { userInterface = { headingIdRed: null, promptIdRed: null, inputIdRed: null, percentIdRed: null, numberIdRed: null, lowIdRed: null, highIdRed: null }; for (var id in userInterface) { userInterface[id] = document.getElementById(id); } currentState = { valueRed: Math.floor(Math.random()*100)+1, numberOfTriesRed: 0, previousRed: undefined }; userInterface.inputIdRed.onchange = handleNewRedShade; window.onpopstate = getHistoryState; modifyWebPage(currentState); } function setHistory(oldState) { if (!history.pushState) { alert ("cannot push to the history object"); return; } var url = "#previous" + oldState.previousRed; history.pushState(oldState, "", url); } function getHistoryState(event) { if (event.state != null) { currentState = event.state; modifyWebPage(currentState); } else { history.replaceState(currentState, "", "#previous" + currentState.previousRed); } } function handleNewRedShade() { var value = parseInt(this.value); if ((value > 0) && (value <= 100)) { currentState.previousRed = currentState.valueRed; currentState.valueRed = value; currentState.numberOfTriesRed++; setHistory(currentState); modifyWebPage(currentState); } else { alert("Please enter a number between 0 and 100"); } } function modifyWebPage(newState) { document.title = "Try a shade between 0 and 100"; userInterface.headingIdRed.innerHTML = document.title; userInterface.percentIdRed.innerHTML = "Red at " + newState.valueRed +" %"; userInterface.numberIdRed.innerHTML = "Number of red shades attempted: "+newState.numberOfTriesRed; var byteColorRed = new Number(newState.valueRed/100.0 * 255); byteColorRed = byteColorRed.toFixed(0); userInterface.lowIdRed.style = "background-color:rgb("+byteColorRed+",0,0)"; userInterface.lowIdRed.style.width = newState.valueRed +"%"; userInterface.highIdRed.style.width = (100-newState.valueRed) + "%"; userInterface.inputIdRed.value = ""; userInterface.inputIdRed.focus(); if (newState.previousRed === undefined) { userInterface.promptIdRed.innerHTML = "Type in a shade of RED and hit Enter"; } else if (newState.previousRed > 100) { userInterface.promptIdRed.innerHTML = "too high, try a number less than or equal to 100"; } else if (newState.previousRed < 0) { userInterface.promptIdRed.innerHTML = "too low, try a number greater than 0"; } else { userInterface.promptIdRed.innerHTML = "Enter a new shade of RED"; } } -->

    Stage 3: Assess

    Stage 4: Act - Supplemental Resources

    • Complete

Afternoon Session II : Lesson 5 continued [3:45 - 5:00pm]

...see Afternoon Session I

 

Daily Checkout Card: