Website Development I

Course Info Class Notes Resources Student Websites

Class No. 13:

Introduction to JavaScript

Today’s Goal

To get started experimenting with JavaScript.


Class Resources

Web Programming book reading for THIS class

Web Programming book reading for NEXT class


Preambles

Rescheduling Thursday’s class – with Mr. DiDominic

A common CSS coding error seen in our last class:

Questions from our last class or the readings or work between classes?


Making Web Pages Dynamic

What Is JavaScript?

“Client-Side” Computing


Writing JavaScript Code

Basic Structure and Use


Basic Statements and Concepts


Variables:  Working with Data


Today’s Exercise

Goal:  To learn initial JavaScript concepts by writing code.

Preamble:  Today’s exercise includes a number of questions.  I encourage you to discuss these questions with another student to try to determine the answers, but of course I am here to help you as well.

    Step 1:  Understanding the Difference Between Strings and Numbers

  1. Create a new file for this exercise.  Initialize it as an HTML file by adding the HTML5 template that we’ve been using throughout this course.
  2. Add a script element in the <body> section of your page:
  3. <script>
    </script>
  4. Inside the script element, add a document.writeln statement:
  5. <script>
      document.writeln( "<p>This is JavaScript.</p>" ) ;
    </script>
    
    Note that the string you are writing contains HTML tags.
  6. Save your file and open it in Microsft Edge.  Be sure to open it through the web server using an address in the format:
  7. http://cssdweb.edu/username/filename.html
  8. Add the following three statements inside the <script> element:
  9. document.writeln( "<p>" ) ;
    document.writeln( 3 + 4 ) ;
    document.writeln( "</p>" ) ;
  10. Save your file and reload it in the browser.  Do you get the result that you expected?  Why or why not?
  11. Now add:
  12. document.writeln( "<p>3 + 4</p>" ) ;
  13. Save your file and reload it in the browser.  Do you get the result that you expected?  Why or why not?
  14. Next, add:
  15. document.writeln( "<p>" + 3 + 4 + "</p>" ) ;
  16. Save your file and reload it.  What happened here?  Why?
  17. Change the code to:
  18. document.writeln( "<p>" + ( 3 + 4 ) + "</p>" ) ;
  19. Save your file and reload it.  What happened here?  Why?  How do the parentheses around ( 3 + 4 ) change the way the code works?
  20. Step 2:  Working with String Variables

  21. Add the following statements to your code:
  22. var strFirstName = "insert your first name here" ;
    var strLastName = "insert your last name here" ;
    document.writeln( "<p>" + strFirstName + strLastName + "</p>" ) ;
  23. Save your file and reload it.  Did it work as you expected?  Why or why not?
  24. Modify the last line you added to include a space between your first and last names.
  25. Step 3:  Working with Numeric Variables

  26. Add the following statements to your code:
  27. var nStudents = 7 ;
    var nInstructors = 2 ;
    document.writeln( "<p>Total = " + nStudents + nInstructors + "</p>" ) ;
  28. Save your file and reload it.  Did it work as you expected?  Why or why not?
  29. Modify the last line to actually do the addition so that your program prints the correct total.
  30. Save your file and reload it.  What result did you get?  How do you fix the code?
  31. Step 4:  Using the Browser Console

  32. Type CtrlShiftI to open your browser’s Development Tools window.  (The long way to get to this window is to click the three dots at the top right of your browser window to open a dropdown menu, pull right on More tools >, and then click Developer tools.)
  33. Click Console to open that tab.
  34. After the > prompt, type 9+1 and press the Enter ↵ key.  Did you get the result you expected?
  35. Assuming that your new page is still displayed and that you completed Step 3 above, type nStudents after the prompt in the console window.  (If you’re viewing a different page, reload the page for Step 3, then type nStudents after the prompt.)
  36. Notice that as you type the console tries to help you by popping up a window with all the things you can enter that match what you’re typing.
  37. What result did you get?  Is it what you expected?  The console is an excellent tool for testing to see whether variables are set to the values you expect them to be set to.
  38. Now type nStudents + nInstructors after the prompt.  This demonstrates that you can do calculations in the console window.  We will see much more powerful uses of the console as we move forward.  This is a very important tool to help you debug your programs.


 


This is Class No. 13.  It was last modified on Saturday, October 21, 2023 at 4:38 PM.  Copyright © 2010-2024 by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for educational purposes with credit to the author.