You are on page 1of 13

Javascript: Looping

TDB2143
TDB2143
pejal71@gmail.com
pejal71@gmail.com
Looping Structure in Javascript

►for
► repetition statement
for repetition statement
–– example
example eg_for_01.html
eg_for_01.html
►while
► repetition statement
while repetition statement
–– example
example eg_while_01.html
eg_while_01.html
►do
► do …… while repetition statement
while repetition statement
–– example
example eg_do_while_01.html
eg_do_while_01.html
Javascript: Array

TAB1033
TAB1033
mnibrahim@gmail.com
mnibrahim@gmail.com
Agenda


► Introduction
Introduction

► Arrays
Arrays

► Declaring and
Declaring and Allocating
Allocating Arrays
Arrays

► Examples Using
Examples Using Arrays
Arrays

► Example 1:
Example 1: Random
Random Image
Image Generator
Generator Using
Using
Arrays
Arrays

► Example 2:
Example 2: Simple
Simple Quiz
Quiz
Introduction

Arrays
►Arrays

–– Data
Data structures
structures consisting
consisting of of related
related data
data items
items
–– Sometimes
Sometimes called
called collections
collections ofof data
data items
items
JavaScriptarrays
►JavaScript
► arrays
–– “dynamic”
“dynamic” entities
entities that
that can
can change
change sizesize after
after
they are
they are created
created
An element
►An
► element maymay be
be referred
referred to
to by
by giving
giving the
the name
name
of the
of the array
array followed
followed by by index
indexofof the
the element
element in in
square brackets
square brackets ([])
([])
…Introduction

The first
►The
► first element
element in
in every
every array
array isis the
the zeroth
zeroth
element.
element.
The ith
►The
► ith element
elementof of array
array cc isis referred
referred to
to as
as
c[i-1].
c[i-1].
Every array
►Every
► array ininJavaScript
JavaScript knows
knows its itsown
own length,
length,
which itit stores
which stores in
in its
its length attribute and
length attribute and can
can be
be
found with
found with the
the expression
expression arrayname
arrayname.length.length
Declaring and Allocating Arrays

JavaScriptarrays
►JavaScript
► arrays are
are Array objects.
Array objects.
Creating new
►Creating
► new objects
objects using
using the
the new operator isis
new operator
known as
known as creating
creating an
an instance
instance oror instantiating
instantiating an
an
object
object
Operator new
►Operator
► new isis known
known asas the
the dynamic
dynamic memory
memory
allocation operator
allocation operator
Creating Two New Arrays
Creating + Initialise Arrays

Arrays can
►Arrays
► can be
be created
createdusing
using aa comma-separated
comma-separated
initialiser list
initialiser list enclosed
enclosed in in square
square brackets
brackets ([])
([])
–– The
The array’s
array’s sizesize is
is determined
determined by by the
the number
number ofof
values in
values in the
the initialiser
initialiser list
list
The initial
►The
► initial values
values of of an
an array
array can
can bebe specified
specified as
as
arguments in
arguments in thethe parentheses
parentheses following
following newnew
Array
Array
–– The
The size
size of of the
the array
array isis determined
determined by by the
the
number of
number of values
values in in parentheses
parentheses
…Creating + Initialise Arrays
Manipulating Array Elements
Example 1: Random Images

eg_random_image.html
►eg_random_image.html

Example 2: Simple Quiz

quiz.html
►quiz.html

You might also like