Modular, loops, strings. Flashcards
difference between parameter and argument.
- argument is more specific than the parameter
- parameters can have multiple arguments
function addTwoNumbers(a, b) {
var result = a + b; alert(result); }
addTwoNumbers(5, 10);
*what is the parameter?
the inputs of a, b
function addTwoNumbers(a, b) {
var result = a + b; alert(result); }
addTwoNumbers(5, 10);
*what is the argument?
the inputs of 5, 10
variable scope
- variables created within a function
- these variables are limited to the function
Suppose you create a function:
function myFunction() { var x = 500
}
Now, if you simply wrote code to alert (x), nothing will happen because X was created within the function and limited to the function.
global variable
- variable created outside of a function.
- can be used anywhere in your code
scripts tag
- used to link other javascript pages together
- think external stylesheet for javascript
javascript script tag
what does it look like?
iteration
the process of looping code
sometimes iteration IS called loop or looping
to use the javascript looping or iteration ability, we replace…
if with while
example:
var a = 1;
while (a >10) {
alert (a)
}
infinite loop
- the result of not incrementing or modifying an index
steps to creating a loop using WHILE
1) set up the index
2) check the condition
3) increment the index
for loop
simplifies the while loop
use the for loop:
var i = 1 while (i
for (var i=1; i
do while loop
executes the loop before checking the condition
change to do while loop
var i = 1 while (i
var i = 1;
do {i++
} while ( i
NaN
Not a number
what displays when javascript does not understand.
var foo = 5; var bar = 5;
alert ( foo + bar);
*what will display? why?
10
the values for foo and bar are numbers and the + represents adding
var foo = "5"; var bar = "5";
alert ( foo + bar);
*what will display? why?
55
the two “5” represents character strings so “5” + “5” will CONCATONATE
var foo = 5; var bar = "5";
alert ( foo + bar);
*what will display? why?
55
if one is a string then that one “takes charge”
var foo = 5; var bar = "5";
alert ( foo * bar);
*what will display? why?
NaN
javascript will not understand a number multiple by a character string
how would you convert the var a = “55” into a variable a being 55
using the Number function
var a = "55" var newVariable = Number(a)
now newVariable is a variable that is = 55
what javascript function would you use to check if a viable is not a number?
isNaN
^ is NOT a number
what javascript function would you use to check if a viable is a number?
!isNan
^ is NOT NOT a number
suppose you have a variable:
var a = “This is a”;
*How do you determine the # of character strings inside variable a
a.length
using the length property