Introduction to Javascript due Tue 03 Oct 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}

Instructions

This assignment continues the pattern and work started on lab day. You will continue adding functions to hw5.js and demonstrating those functions (in your HTML code) each in separate sections labeled with meaning h2 heading. The output should include what the functions calls look like and what they return. You should include multiple meaningful test cases for each problem.

Write and demonstrate the following functions:

  1. (4 pts) Write a Javascript function called alphabetizeWords that accepts a string of zero or more words (separated by spaces). The function should rearrange the string so the words are placed in alphabetical order (joined by spaces). You can assume all words are composed only of lowercase letters and that there is no punctuation.

    For example alphabetize("hello to the earth") should return the string "earth hello the to". HINT: use split to move the words from the string into an array, sort the array, and the rejoin them.

  2. (6 pts) Write a Javascript function called reverseInt that will accept as a parameter an integer and will return the number with its digits in reverse order. NOTE: When calling the function you must pass a number (not a string).

  3. (6 pts) Write a Javascript function called randomPassword that accepts an integer n in the range 4 to 30 and will return a randomly generated password have n characters. The password should be composed of random characters from the set of lower-case letters, upper-case letters, digits, and symbols derived from holding SHIFT while typing digits on the keyboard. If n is out of range then it should produce a 10 character password.

  4. (4 pts) Write a Javascript function called prefixSum that given an array of zero or more numbers will calculate and return an array containing the prefix sum of the parameter. An example of a prefix sum:
    a= [4, 5, 2, 2, 3]
    prefix_sum= [4, 9, 11, 13, 16]
    

    A value at position i of a prefix sum array is the sum of the values at positions 0 .. i of the original array.

Grading

Your hw05 directory on the server should contain hw5.js that contains the functions you wrote and hellojavascript.html that demonstrates them.

As always, have fun!

Quick Links