|
|||||
![]() Introduction
to Computing CS101
VU
document.write(
parseInt( string1 ) ) ;
3
document.write(
"<BR>" ) ;
NaN
string2 =
"$100.00" ;
23
document.write(
parseInt( string2 ) ) ;
document.write(
"<BR>" ) ;
string3 = " 23 "
;
document.write(
parseInt( string3 ) ) ;
1.
Parses the string argument; returns
an integer
2. If it
encounters a non-numeral - anything other
than (+,-) or (0-9) - it ignores it
and all succeeding
characters,
and returns the integer value
parsed up to that
point
3. If the
first character cannot be converted to a number,
parseInt returns NaN
4. parseInt
truncates numbers to integer
values
5. Leading
and trailing spaces are
ignored
parseFloat(
)
Syntax:
parseFloat ( string )
string1 =
"3.14159" ;
document.write(
parseFloat( string1 ) ) ;
document.write(
"<BR>" ) ;
string2 =
"$100.00" ;
document.write(
parseFloat( string2 ) ) ;
document.write(
"<BR>" ) ;
string3 = " 23E-15
" ;
document.write(
parseFloat( string3 ) ) ;
1.
Parses the string argument; returns
a FP number
2. If it
encounters a character other
than
A sign
(+,-)
A
numeral (0-9)
A
decimal point
An
exponent
it
returns the value up to that
point, ignoring that and
all succeeding
characters
3. If the
first character cannot be converted to a number,
parseFloat returns
NaN
4. Leading
and trailing spaces are
ignored
29.6
Scope of Variable
Defining
the space in which a variable is effective is
known as
defining
the scope of a variable. A variable can
be either local
or
global
in
scope.
Local
and Global Variables
Local
or Function-level Variable
Effective
only in the function in which they
are declared
Global
Variables
Visible
everywhere on the Web
page
Example
function
factorial( n ) {
product
= 1 ;
for (
k = 1 ; k <= n ; k = k + 1 ) {
product
= product * k
}
return
product ;
}
n =
window.prompt( "Enter a number ", "" )
;
document.write( "k
= ", k ) ;
document.write(
"<BR>" ) ;
189
![]() Introduction
to Computing CS101
VU
document.write(n,
"! = ", factorial( n ) ) ;
function
factorial( n ) {
product
= 1 ;
for (
k = 1 ; k <= n ; k = k + 1 ) {
product
= product * k
}
return
product ;
}
n =
window.prompt( "Enter a number ", "" )
;
document.write( "k
= ", k ) ;
document.write(
"<BR>" ) ;
document.write(n,
"! = ", factorial( n ) ) ;
function
factorial( n ) {
product
= 1 ;
10! =
3628800
for (
k = 1 ; k <= n ; k = k + 1 ) {
k = 11
product
= product * k
}
return
product ;
}
n =
window.prompt( "Enter a number ", "" )
;
190
![]() Introduction
to Computing CS101
VU
document.write(n,
"! = ", factorial( n ) ) ;
document.write(
"<BR>" ) ;
document.write( "k
= ", k ) ;
function
factorial( n ) {
var k
;
product
= 1 ;
for (
k = 1 ; k <= n ; k = k + 1 ) {
product
= product * k
}
return
product ;
}
n =
window.prompt( "Enter a number ", "" )
;
document.write(n,
"! = ", factorial( n ) ) ;
document.write(
"<BR>" ) ;
document.write( "k
= ", k ) ;
191
![]() Introduction
to Computing CS101
VU
`k'
is a Local Variable
`k' is
not declared or used in the
main code
Instead, it is
declared within the function `factorial'
only
`k' i
local to the `factorial' function, and
does not hold any
meaning outside that
function
function
factorial( n ) {
Here `product'
has been made a
local
var k,
product ;
variable as
well
product
= 1 ;
for (
k = 1 ; k <= n ; k = k + 1 ) {
product
= product * k
}
return
product ;
}
n =
window.prompt( "Enter a number ", "" )
;
document.write(n,
"! = ", factorial( n ) ) ;
document.write(
"<BR>" ) ;
document.write(
product ) ;
Local
Variables
Declaring
variables (using the var keyword) within
a function, makes them local·They
are available only
within
the function and hold no
meaning outside of it
Global
Variables
All
other variables used in a
Web page (or window)
are global
They
can be manipulated from the main
code as well as from any of
the functions
They
include:
All
variables declared in the main
code
All
variables used but not
declared in the main
code
All
variables used but not
declared in any of the functions
defined on the Web page (or
window)
function
writeList( heading, words )
{
document.write(
heading + "<BR>" ) ;
for (
k = 0 ; k < words.length ; k = k + 1 ) {
Would the
functionality
document.write(
words[ k ] + "<BR>" ) ;
change if we
delete the
argument
`words' from these
}
4
places?
}
words
= new Array ( 10 ) ;
for (
k = 0 ; k < words.length ; k = k + 1 ) {
words[ k ] =
window.prompt( "Enter word # " + k, "" )
;
}
writeList(
"Unsorted Words:", words ) ;
words.sort(
) ;
writeList(
"Sorted List:", words )
;
words.reverse(
) ;
writeList(
"Reverse-Sorted List:", words )
;
Local
vs- Global
Global
variables can make the logic of a
Web page difficult to
understand
Global
variables also make the
reuse and maintenance of
your code much more
difficult
192
![]() Introduction
to Computing CS101
VU
Glob
Local
u
a
var a, b
;
m
b
p=q+2;
p
c
var u
;
q
d
document.write( m )
;
r=s;
x
y
var c, d
;
r
x=y*y;
s
Variables declared
within functions are local;
all others global
HEURISTIC:
If it's
possible to define a variable as
local, do
it!
During
Today's Lecture ...
We
looked at functions and
their use for solving simple
problems
We
became familiar with a
couple of JavaScript's built-in
functions
We
became familiar with the
concept of local and global
variables
Next
Web Dev Lecture:
Event
Handling
We'll
learn to appreciate the concept of event
driven programming
We
will produce solutions for
simple problems using
various event handlers
193
Table of Contents:
|
|||||