TITLE

IF NECESSARY

Goals

By the end of today, you will be able to:

TOPIC 1

with three sub-topics:

HTML

Here's a sample

        <p style="color: gold">
          Now is the winter of our discontent
          turned glorious summer by this <em>son</em> of York.
        </p>
      

CSS

Here's a sample

        pre {
            color: chartreuse;
        }
      

JavaScript

        // I have no idea what this function might do
        function foo(x, y) {
           var i;
           for( i = 0 ; i < x; i++ ) {
              if( g(i) ) {
                 return i;
              }
           }
       }
      

Quiz Question Nr. 1

QUESTION

  1. parameter

  2. argument

  3. global variable

  4. local variable

Quiz Question Nr. 2

You are given two alternative definitions of a function:

Definition 1

function displayDate(){               
  var today = new Date();                     
  document.querySelector("#date").innerHTML = today.toLocaleDateString();
}

Definition 2

function displayDate(dateStr, elemID){               
  var date = new Date(dateStr);                     
  document.querySelector(elemID).innerHTML = date.toLocaleDateString();
}

Which of the following statements is true? Explain your answer.

  1. The two definitions are equivalent.

  2. Definition 1 is better than Definition 2.

  3. Definition 2 is better than Definition 1.


Quiz Question Nr. 3

Which of the following function/method calls causes a side effect instead of returning a value.

  1. Math.random()
    
  2. prompt("Enter your name:")
    
  3. document.querySelector("img")
    
  4. alert("Hello")
    

Quiz Question Nr. 4

What is the output of executing the following code:

function isSquare(a, b, angle){ 
  if (a == b && angle == 90) 
    return true
}
alert(isSquare(10,11,90));

  1. true
    
  2. false
    
  3. NaN
    
  4. undefined
    

Exercise 1

Modify the function amountToPay from the last quiz question to include a second parameter, stateTax, so that it can calculate the amount to pay depending on a buyer's state. You can that try to call the function a few times such as below:

alert(amountToPay(100, 8.47))   // NY tax is 8.47%
alert(amountToPay(50, 8.89))    // LA tax is 8.89%

Challenge

As you know, when a list has an even number of items, the median is the mean of the two middle values. Extend the findMedian function above to do the right thing depending on whether the given array has an even or odd number of elements. The remainder operator % is useful for finding if a number is odd or even.

Summary

We hope that after these activities you have a good understanding of:

  • how to talk about functions and know the meaning of parameter, argument, return, etc.
  • scope, global variables, and local variables
  • define simple functions with parameters
  • define functions that return a value

Solutions

Will be posted later, visit again after .