Program 1: basic in-browser JavaScript

Programming the Web: An Introduction (Spring 2021)

Due date: Wednesday, February 10 at 5pm EST


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 when you open your local copy of hw1.html in your browser.)

  2. Complete the function secondsPerCentury 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 numberProblem that corresponds to the following prose description:

    Ask the user for a number. Ask the user for another number. Display the difference between the square of the sum of the two numbers and the sum of the squares of the two numbers.

    For example, if the user enters 7 for the first number and 3 for the second number then it should display 42 as the answer since 100 is the square of the sum, and 58 is sum of the squares (49 + 9).

    Use variables and assignments. For this particular problem, keep your arithmetic expressions very simple: no more than a single arithmetic operation per assignment statement.

  4. An animation is a rapidly displayed sequence of images. (Old-fashioned cartoons would display hand-drawn images at a rate of anywhere from 4 to 12 images per second.) For this problem, we assume that an animation consists of 6000 distinct images and, given the length of the animation, we compute the animation frame rate (number of drawings displayed per second). Write a program that asks the user for the length of an animation in minutes (not seconds) and then computes and displays the rate. For example, if the user enters that an animation was 1 minute long, the program should display “Animation rate: 100 frames per second” as the result and if the length is 3 minutes (180 seconds) then it would display something less round like “Animation rate: 33.333333333333336 frames per second”.

    (Optional) challenge: Round the rate down to a whole number. (For example, 3 minutes would lead to displayed rate of 33 frames per second90 seconds meaning 7 drawings were displayed.) To round down, use subtraction and the remainder (%) operator.

  5. Here is a simple arithmetic algorithm in JavaScript:

      let n = parseFloat(prompt('Enter a number: '));
      let m = (5 * n) + 2;
      alert('Result: ' + m);

    You can try it here. Write the inverse function, inverse5n2 to match this behavior: If the user enters 17 it displays the result as 3 (since seventeen minus two is fifteen and fifteen divided by five is three) and if the user enters 23 it returns 4 (not 4.2). The idea is to effectively perform “integer division” meaning have one integer divided by another, but 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 (+, -, *, /, %).

  6. Write a function concatLen that asks the user for two pieces of text and then 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.