Introduction to (Web) Programming

Program 1: basic in-browser JavaScript

Due date: Thursday, February 7 at 1:30pm


What this assignment is about

The problems themselves

  1. Modify the function simpleAction so that it displays the message “This button works!” when triggered. You can test it by clicking on the first (left-most) button on your page. (Not on this page! On the page you view in your browser that corresponds to the HTML file where you are doing all your work.)

  2. Complete the function secondsPerCentury below so that it computes and displays (via the alert function) the approximate number of seconds in a century. (By approximate, I mean assume a twenty-four day and ignore leap days.) Include, as a comment, the resulting total number of seconds.

  3. Write a function wordProblem that corresponds to the following prose description:

    Ask the user for a number called x. Display the difference between the square of two more than x and the square of one more than x.

    Use variables and assignments. Keep your arithmetic expressions simple: no more than a single arithmetic operation per assignment statement.

  4. Here is a simple arithmetic algorithm in JavaScript:

    let n = parseInt(prompt("Enter a number: "));
    let m = (3 * n) + 1;
    alert("The result is " + m);

    You can try it here. Write the inverse function, inverse3n1 to match this behavior: If the user enters 16 it displays the result as 5 (since sixteen minus one is fifteen and fifteen divided by three is five) and if the user enters 23 it returns 7 (not 7.333…). The idea is to effectively perform “integer division” meaning have one integer divided by another throw away any remainder (i.e, round down). You can (and should) achieve this just by using some of the five arithmetic operations we have discussed (+, -, *, /, %).

  5. Write a function concatLen that asks the user for two pieces of text, displays, one dialog box at a time:

    1. the length of the first string
    2. the length of the second string
    3. the concatenation of the two strings
    4. the length of that concatenation.


        Enter some text: hello
        Enter some more text: nwheels
        The first phrase has 5 characters
        The second phrase has 7 characters
        The concatenation of those phrases is: hellonwheels
        That has 12 characters

    In a comment, explain why it makes sense for JavaScript to use + for both the usual addition of two numbers and the concatenation of two strings.