mis3502_class13_javascript3x

Download Report

Transcript mis3502_class13_javascript3x

A JavaScript subset for JQuery Users
(Continued)
MIS 3502, Fall 2016
Jeremy Shafer
Department of MIS
Fox School of Business
Temple University
10/13/2016
1
Last time…
Last time we looked at JavaScript we learned about…
Slide 2
A couple new things…
1. The difference between function expressions and
function declarations
2. Use of the standard $ function
3. The “use strict” option
4. Adding logic to the onload event handler
5. Adding event handlers for click and double click
Slide 3
This time…
1. Chaining
2. Methods for working with numbers
3. String methods
4. While loop syntax
5. For loop syntax
6. JavaScript Arrays
Slide 4
Chaining
An HTML tag that defines a text box
<input type="text" id="sales_amount">
Without chaining
var salesAmount =
document.getElementById("sales_amount");
salesAmount = salesAmount.value;
salesAmount = parseFloat(salesAmount);
With chaining
var salesAmount = parseFloat(
document.getElementById("sales_amount").value);
Slide 5
Methods for working with numbers
Methods of the window object
for working with numbers
parseInt(string)
parseFloat(string)
The parsing that’s done by the parse methods
• Only the first number in the string is returned.
• Leading and trailing spaces are removed.
• If the first character cannot be converted to a number, NaN is
returned.
• NaN is a global attribute in JavaScript.
(See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN )
Slide 6
Methods for working with numbers (2)
One method of the Number object
toFixed(digits)
For Example:
var num = 5.56789;
var n = num.toFixed(2); // n = 5.57
Slide 7
Some fancy chaining
Other examples of chaining
var salesAmount =
parseFloat(document.getElementById("sales_amount").value).toFixed(2);
document.getElementById("first_name").value = "";
document.getElementById("first_name").focus();
Slide 8
Methods of the String object
Properties and methods of a String object
One property of a String object
length
A few of the methods of a String object
indexOf(search,position)
substr(start,length)
substring(start,stop)
toLowerCase()
toUpperCase()
Examples that use a String object
var
var
var
var
var
name = "Grace Hopper";
nameUpper = name.toUpperCase();
nameLength = name.length;
// nameLength = 12
index = name.indexOf(" ");
// index = 5
firstName = name.substr(0, index);
Slide 9
Loops….
Slide 10
A JavaScript While Loop
The syntax of a while loop
while ( condition ) { statements }
A while loop that adds the numbers
from 1 through 5
var sumOfNumbers = 0;
var numberOfLoops = 5;
var counter = 1;
while (counter <= numberOfLoops) {
sumOfNumbers += counter;
counter++;
// adds 1 to counter
}
alert(sumOfNumbers);
// displays 15
Slide 11
A JavaScript for loop
The syntax of a for loop
for ( counterInitialization; condition; incrementExpression )
{
statements
}
A for loop that calculates the future value
of an investment
var
var
var
var
for
investment = 10000;
annualRate = 7.0;
years = 10;
futureValue = investment;
( var i = 1; i <= years; i++ ) {
futureValue += futureValue * annualRate / 100;
}
alert (futureValue);
// displays 19672
Slide 12
Arrays
Slide 13
The syntax for creating an array
Using the new keyword with the Array object name
var arrayName = new Array(length);
Using the brackets literal
var arrayName = [];
A statement that creates an array
var totals = [];
Slide 14
Arrays are objects too
The syntax for getting the length property
of an array
arrayName.length
A statement that gets the length of an array
var count = totals.length;
How to add a value to the end of an array
totals[totals.length] = 135.75;
Or…
Totals.push(135.75);
Slide 15
It’s time for an experiment!
Slide 16