LD05: Intro to Forms and Javascript due Thu 28 Sep 13:20

\begin{purpose}
In this assignment you will:
\begin{itemize}
\item Write Jav...
...s, strings, arrays, and functions
in Javascript.
\end{itemize}
\end{purpose}

Preparation for Lab Day

In preparation for lab day, do these actions:
  1. In your repository create a directory named hw05 and work in it.
  2. Copy all files from your hw04 directory into hw05.
  3. Copy the document https://josephus.hsutx.edu/classes/w1/source/hellojavascript.html into hw05.
  4. Load your copy of the hellojavascript.html page in a browser and verify that the Javascript code is running properly and that you can see all three outputs.
  5. Modify the code in hellojavascript.html so that it prompts the user enter length, width, and height of a room and calculates the surface area and volume of the room. Display your answers in the console log (i.e., not in a popup or on the page itself).

On Lab Day

NOTE: For the exercises in this lab day and homework assignment you should try to solve the problems by using only the provided command-sheets and your class notes rather than searching on the internet for pre-canned solutions. Remember: the goal is for you to learn Javascript; not to “get the assignment done.” The best way to learn is to figure stuff out on your own!

  1. Begin by showing your prelab program to the instructor.

  2. Copy the Javascript code you wrote in the prelab to an external document named hw5.js. Modify hellojavascript.html so that it loads the program from hw5.js. Verify the program works.

  3. Modify the code in hw5.js to put it inside a function named area(). Then call the function in hellojavascript.html. NOTE: For the remainder of this lab day and homework you will be creating functions in hw5.js and calling them from hellojavascript.html. Once you have this working you can comment out the call to the area() function and continue.

  4. For the next several steps you will be writing a function that accepts one or more parameters and returns something. In front of each section put an h2 heading to show what you are working on. Then add code to show how you are calling the function and what it is returning. There are explicit instructions to accomplish these things given in the next step.

  5. In your hw5.js document, write a JavaScript function called countChars that accepts a string and a letter as parameters and returns the number of occurrences of the specified letter within the string. Write code in your HTML document to provide an h2 heading that says “Demonstrating countChars”. Solve this by cycling through the string as if it is an array and counting matches. Then call and display the results of countChars using the following parameters:
    • countChars('This is so very interesting','e')
    • countChars('This is so very interesting','g')
    • countChars('This is so very interesting','z')

    The output should be on the page (not in a popup and not in the console log) should look something like this:

    Demonstrating countChars

    countChars('This is so very interesting','e'): 3

    countChars('This is so very interesting','g'): 1

    countChars('This is so very interesting','z'): 0

    Demonstrate your solution to the instructor.

  6. Write a Javascript function called findLongestWord that accepts a string as a parameter and finds and returns the longest word within the string. Assume words are separated by spaces. If multiple words have the same longest length then return the first one. If the parameter is an empty string the function should return an empty string (and not crash).

    Add a descriptive h2 heading and demonstrate the function is working properly by calling it with multiple parameters to test various cases. Your output should show the parameters passed and value returned. NOTE: This function should not perform any output (or input).

    Suggested solution: split the string on the space character into an array (use built-in split function). Then look through array to find longest word.

  7. Show your work to the instructor. Then commit and push your work. If you finish early you can start work on the homework assignment.