Functions

advertisement
Functions
Arrays – Patterns For Usage
2
Arrays – Patterns: 1 Item At A Time
3
 Some array problems can be solved by considering each item, one by one, without
thinking about the rest of the array
 Examples:
 Print each item
 Find a particular item in the array (or determine that it's not present)
 For these we just need to use the 'iterate through the array pattern'
 Let's look at the 'Print each item' example (
4
Here's what the 'main' / on click handler looks like:
function GetUserInput( ) {
var numAsInput = $("#num1").val();
var num = Number( numAsInput );
if( isNaN(num) ) {
$("#output").html( "Error! " + numAsInput + " is not a number!");
throw new Error(numAsInput + " is not a number!");
}
return num;
}
$(document).ready( function() {
$("#functionDemo").click( function() {
var num = GetUserInput();
var spaced = SpaceNumberOut(num);
SetOutput( spaced );
});
});
• Notice that each function has a single, well-defined purpose
5
 Some array problems can be solved by considering each item, one by one, without
thinking about the rest of the array
 Examples:
 Print each item
 Find a particular item in the array (or determine that it's not present)
 Single-item pattern
 print each item
 Find a particular item
 EXERCISES
 print matching items?
6
 The lecture will be recorded and posted to OneDrive for your later reference.
7
8
Download